powershell - 使用参数从 powershell 中调用 powershell 脚本时遇到问题

标签 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 的方法之后)并且忘记了远程运行命令。感谢您对此事的帮助。
谢谢

最佳答案

您不需要使用 powershell.exe 运行 PowerShell 脚本从另一个 PowerShell 脚本调用它们时。只需使用调用运算符 (&)。另外,我会使用 Invoke-Command用于运行具有不同凭据的内联内容。

请注意,脚本 block 不会自动知道脚本其余部分中的变量。您需要通过 -ArgumentList 将它们传递到脚本 block 中。范围。这很可能是当您运行 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)。

关于powershell - 使用参数从 powershell 中调用 powershell 脚本时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201447/

相关文章:

powershell - 如何将 powershell 脚本设置为仅显示简短的错误描述

powershell - 在PowerShell中查找当前的当前季度以添加格式为 “yyyyqq”的文件名

powershell - Powershell和BizTalk

powershell - 在 WASP/powershell 中通过窗口获取进程对象?

powershell - 以管理员身份运行 PowerShell 脚本而不输入密码

powershell - 使用 "Put"参数 : "0" on Win32_TerminalServiceSetting 调用 ""的异常

powershell - 如何快速更改 shell 文件夹以匹配当前打开的文件

powershell - Wscript.Shell 快捷方式是否支持超过 3 个字符的文件扩展名

powershell - PowerShell替换文件夹名称

Windows Powershell 在网络适配器上设置 IP 地址