【问题标题】:pssession function not workingpssession功能不工作
【发布时间】:2014-03-05 23:22:45
【问题描述】:

我有一个特殊的用例,它需要我的大量 powershell 脚本来更改它们运行的​​用户上下文。我有可靠运行的代码,但我试图将其放入一个函数中,而不是在每个脚本中重复它。

单独来说,这个函数中的行可以工作,但是当作为函数运行时,脚本没有遇到错误,但我没有得到任何结果。

如果有人能发现我在这方面遗漏了什么,我将不胜感激。

            Function Invoke-AsUser() {
            Param(
                [Parameter(Mandatory=$true)]
                [String]
                $ScriptBlock
                ,
                [Parameter(Mandatory=$true)]
                [String]
                $RunAsUser
                ,
                [Parameter(Mandatory=$true)]
                [String]
                $RunAsPassword
                ,
                [Switch]$credSSP=$false
                )

                #Get the execution policy and store
                    $ExecutionPolicy = Get-ExecutionPolicy
                #Set up WSMANCredSSP
                    Enable-WSManCredSSP -Role Server -Force | out-null
                    Enable-WSManCredSSP -Role Client -DelegateComputer $env:COMPUTERNAME -Force | Out-Null
                #Set WSMAN MaxMemoryPerShell
                    #The following Prevents "Out of memory" errors from occurring when running commands through a remote pssession.
                    set-item wsman:localhost\Shell\MaxMemoryPerShellMB 512
                #Enable PSRemoting
                    try
                    {
                        enable-psremoting -force | Out-Null
                    }
                    catch
                    {
                        Write-Host "Enabling PSRemoting returned a non fatal exception."            
                    }
                #Create Credentials
                    $cred = new-object System.Management.Automation.PsCredential($runasuser,(ConvertTo-SecureString $RunasPassword -AsPlaintext -force))
                    $cred

                #Create the pssession
                    If ($credSSP -eq $true) {
                            try {
                                write-host "Authenticating with CredSSP"
                                $s = new-pssession -ComputerName $ENV:COMPUTERNAME -Credential $cred -Authentication Credssp
                            }

                            Catch {
                                    Write-Error "Creating new-pssession failed"
                            }
                        }
                    Else {
                            try {
                                    Write-Host "Authenticating without CredSSP"
                                    $s = new-pssession -ComputerName $ENV:COMPUTERNAME -Credential $cred
                                }

                                Catch {
                                        Write-Error "Creating new-pssession failed"
                                }
                        }

                #Invoke the command using pssession
                    Write-host "Running Scriptblock as $RunAsUser"
                    Invoke-Command -Session $s -ScriptBlock {Set-executionpolicy Bypass -Force}
                    Write-Host $ScriptBlock
                    invoke-command -Session $s -ScriptBlock { $ScriptBlock }
                    Write-host "Running Scripblock as $RunAsUser has finished."

                #set executionpolicy back to original
                    $ScriptBlock = "Set-ExecutionPolicy $ExecutionPolicy -Force"
                    Invoke-Command -Session $s -ScriptBlock {$ScriptBlock}

                #Disable psremoting
                    Disable-PSRemoting -force -WarningAction SilentlyContinue
            }

            $ErrorActionPreference = "Stop"
            Invoke-AsUser -ScriptBlock "& C:\temp\test.ps1" -RunAsUser $ENV:RUNASUSER -RunAsPassword $ENV:RUNASPASSWORD -CredSSP

【问题讨论】:

    标签: powershell windows-server-2008 windows-server-2012 invoke-command


    【解决方案1】:

    从您的脚本中删除“|out-null”,您应该会获得更好的调试输出。

    【讨论】:

    • 谢谢。我在调试时删除了 |out-null,但已将其清理以在此处发布。
    • 我已经找到了问题所在,但没有找到解决办法。事实证明,函数参数“Scriptblock”没有被传递到行 invoke-command -Session $s -ScriptBlock { $ScriptBlock } 如果我将行更改为 invoke-command -Session $s -Filepath $filepath 它作品。但是,这限制了功能,因为我无法将参数传递给 $filepath 中的脚本。
    • 尝试将参数类型更改为 [scriptblock] 然后直接传递参数例如-Scriptblock $scriptblock.
    • 请记住,invoke-command 脚本块是完全隔离的内存空间。因此,如果您尝试在该脚本块内调用脚本级变量,当您的 $scriptblock 变量被解释为 $null 时,您将失败。您可能可以将您的脚本块作为参数传递给调用命令脚本块,但我通常只是编写一个完全独立于该脚本块内的脚本。
    【解决方案2】:

    我想通了。

    Invoke-Command 需要一个“System.Management.Automation.ScriptBlock”类型的参数,但接收的是一个 system.string。

    当我深入挖掘时,我得到的错误是: 无法处理参数“ScriptBlock”的参数转换。无法将“System.String”类型的“echo”hello”值转换为“System.Management.Automation.ScriptBlock”类型。

    为了解决这个问题,我添加了以下行:

            #Creating a system.management.automation.scriptblock object from a string
            $ScriptObj = ([ScriptBlock]::Create($ScriptBlock))
            Invoke-Command -Session $s -ScriptBlock $ScriptObj
    

    现在一切都如我所愿。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多