无法使用参数从powershell中调用powershell脚本

时间:2015-01-28 20:09:58

标签: powershell

我在这个问题上花了最后4个小时,非常感谢您提出的任何意见。

我需要使用不同的凭据调用powershell脚本并将参数传递到该脚本。 在安装WISEScript中包含的程序之后,此脚本将启动以收集计算机的AD帐户并将其从特定的AD安全组中删除。不幸的是,由于脚本在本地运行,我不能在PowerShell中使用ActiveDirectory模块,因为我们环境中的所有机器都没有RSAT。 初始脚本是从计算机上的提升帐户运行的:

$creds = New-Object System.Management.Automation.PsCredential("DOMAIN\USER", (ConvertTo-SecureString "Password" -AsPlainText -Force))
$ProfileGUIDS = Get-ChildItem 'hklm:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid'
$Groups = [ADSI]"LDAP://CN=Group4d_test,OU=GroupMigrationTesting,OU=TestOU,OU=US,DC=DOMAIN",[ADSI]"LDAP://CN=Group3d_test,OU=GroupMigrationTesting,OU=TestOU,OU=US,DC=DOMAIN"
Function Get-DistinguishedName ($strUserName) 
{  
    $searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]'') 
    $searcher.Filter = "(&(objectClass=User)(samAccountName=$strUserName))" 
    $result = $searcher.FindOne() 
    if ($result)
    {
        Return $result.GetDirectoryEntry().DistinguishedName 
    }
} 

forEach ($GUIDkey in $ProfileGUIDS)
{
    $GUID = Out-String -InputObject $GUIDKey
    $index = $GUID.IndexOf("S-1")
    $GUID = $GUID.Substring($index)
    $GUID = $GUID.Substring(0,128)
    $index = $GUID.IndexOf(" ")
    $GUID = $GUID.Substring(0,$index)
    $Profile = "hklm:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$GUID"
    $ProfileItems = Get-ItemProperty $Profile
    $SAM = $ProfileItems.ProfileImagePath
    $index = $SAM.LastIndexOf("\")
    $index ++
    $SAM = $SAM.substring($index)

    $UserDN = Get-DistinguishedName $SAM
    $User = [ADSI]"LDAP://$UserDN"
    if($User -ne $null)
    {
        forEach($group in $groups)
        {

这里是我需要使用不同凭据调用第二个脚本的地方。

这是RemoveUsers.ps1,我需要使用不同凭据运行的脚本:

param
(
    [string]$group = "MyDefaultSAM",
    [string]$user = "MyDefaultUser"
)
$Group.remove($User.ADsPath)

我试过了:

 start-process powershell.exe -Credential $creds -NoNewWindow -ArgumentList "Start-Process $PSSCriptRoot\RemoveUsers.ps1 -Verb

这将运行脚本但是我不能指定任何参数

powershell.exe -file "$PSScriptRoot\RemoveUsers.ps1" -user $user -group $group

这会使用参数调用脚本,但不允许使用-Credentials开关

我也尝试过:

$job = Start-Job -ScriptBlock { 
powershell.exe -file "$PSScriptRoot\RemoveUsers.ps1" -user $user -group $group
} -Credential $creds

这样运行但似乎无法正常工作,因为用户仍在AD组中。

感谢任何帮助。

谢谢 - 杰夫

****更新**** 谢谢你的信息。当我添加更改时,建议我收到错误

Invoke-Command : Parameter set cannot be resolved using the specified named parameters 

正如我在网上找到的那样,没有-Computer开关就不能使用-Credential开关。如果我为计算机指定$ env:COMPUTERNAME或localhost,则会收到错误

\RemoveUsers.ps1 is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again

如果我删除-Credential开关并向所有人打开AD组,我可以避免此问题。此时,我不需要提升新的PowerShell脚本,并可以添加相同的命令。如果我无法使用Invoke-Command解决问题,这可能就是我要做的。

****更新**** 我最终必须做的是在参数列表中使用-Authentication Credssp,因为通过Invoke-Command使用AD模块存在问题。此外,我必须启动Win-RM服务,启用WSMacCredSSP(每台机器上的-role客户端,并在连接到的服务器上添加DelegateComputer条目和-role服务器)。只有在服务启动并且为WSManCredSSP创建了条目后,我才能使用Invoke-Command开关并让AD模块正常工作。
这当然会让事情变得更加复杂,我决定只在每台PC上安装AD模块(在没有RSAT的情况下找到一种方法)并忘记一起远程运行命令。感谢您对此事的帮助。 感谢

1 个答案:

答案 0 :(得分:0)

从其他PowerShell脚本调用PowerShell脚本时,无需使用powershell.exe运行PowerShell脚本。只需使用调用运算符(&)。另外,我使用Invoke-Command来运行内联不同凭据的内容。

请注意,scriptblock不会自动了解脚本其余部分中的变量。您需要通过-ArgumentList参数将它们传递到scriptblock中。这很可能是当您将RemoveUsers.ps1作为工作运行时删除不起作用的原因。

这样的事情应该有效:

Invoke-Command -ScriptBlock {
  & "$PSScriptRoot\RemoveUsers.ps1" -user $args[0] -group $args[1]
} -ArgumentList $user, $group -Credential $creds -Computer $env:COMPUTERNAME

但这需要PSRemoting(以管理员身份运行Enable-PSRemoting。)

相关问题