【问题标题】:'Session' Parameter is null or empty in PowerShell scriptPowerShell 脚本中的“会话”参数为空或为空
【发布时间】:2015-03-06 20:20:04
【问题描述】:

我对 PowerShell 非常陌生(请原谅我的无知),我正在尝试在我的域中的多台计算机上远程安装程序。目前,我只是想让它在一台计算机上工作。下面的脚本是在网上找到的,适合我的需要。昨天它工作,但今天它抱怨会话参数。

我不完全理解“会话”,但我已确保客户端计算机上的 winrm 服务正在运行,并且我已调用 Enable-PSRemoting -force

这是脚本:

$computers = Get-Content "c:\tmpPS\computers.txt"
$rs = Get-PSSession
Get-PSSession | Get-Member
######
## Functions
################

foreach ($comp in $computers)
{
    Write-Host "should work with $comp"
}

PushMSI
RemoteConnect
InstallMSI

Function PushMSI {
 Write-Host "------------------------------------------------"
 Write-Host "This will copy the MSI file from localhost c:\tmpPS\"
 write-Host "------------------------------------------------"
 Write-Host ""
 Write-Host ""
 foreach ($comp in $computers)
{
   Copy-Item -path "c:\tmpPS\clientInstall.msi" -Destination \\$comp\c$\tmpPS

}
}

Function RemoteConnect
{
 Write-Host "------------------------------------------------"
 Write-Host "This will establish a PSSession with all computers in c:\temp\computers.txt"
 write-Host "------------------------------------------------"
 Write-Host ""
 Write-Host ""
Get-Content C:\tmpPS\computers.txt | New-PSSession -ThrottleLimit 50
}

Function InstallMSI
{
 Write-Host "------------------------------------------------"
 Write-Host "This will Install UPS Update on all computers with an Established PSSession"
 write-Host "------------------------------------------------"
 Write-Host "After the Install PSSessions will be removed"
 Write-Host ""

Invoke-Command -Session $rs -ScriptBlock {invoke-item "c:\tmpPS\ClientInstall.msi"}
}

Get-PSSession | Remove-PSSession

这是输出:

PS C:\Users\Me> C:\tmpPS\remoteInstall.ps1
Get-Member : No object has been specified to the get-member cmdlet.
At C:\tmpPS\remoteInstall.ps1:3 char:17
+ Get-PSSession | Get-Member
+                 ~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

should work with eSignWin81.informa.local
------------------------------------------------
This will copy the MSI file from localhost c:\tmpPS\
------------------------------------------------


------------------------------------------------
This will establish a PSSession with all computers in c:\temp\computers.txt
------------------------------------------------



 Id Name            ComputerName    State         ConfigurationName     Availability
 -- ----            ------------    -----         -----------------     ------------
  6 Session6        eSignWin81.i... Opened        Microsoft.PowerShell     Available
------------------------------------------------
This will Install UPS Update on all computers with an Established PSSession
------------------------------------------------
After the Install PSSessions will be removed

Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then try 
the command again.
At C:\tmpPS\remoteInstall.ps1:49 char:25
+ Invoke-Command -Session $rs -ScriptBlock {invoke-item "c:\tmpPS\ClientInstall.ms ...
+                         ~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

【问题讨论】:

    标签: session powershell powershell-remoting


    【解决方案1】:

    尽量不要使用Write-Host,因为它会破坏管道并杀死小狗。

    我已尝试稍微改进您的脚本,以便您更好地理解结构背后的逻辑。它尚未经过测试,但应该可以解决问题。

    # First parameters
    [CmdletBinding()]
    Param(
        [ValidateScript({Test-Path $_ -PathType leaf})]
        $ComputerList = "c:\tmpPS\computers.txt",
        [ValidateScript({Test-Path $_ -PathType leaf})]
        $MSI = "c:\tmpPS\clientInstall.msi"
    )
    
    # Then functions
    Begin {
        Function Copy-MSI {
             foreach ($Com in $Computers) {
               Copy-Item -path $MSI -Destination "\\$Com\c$\tmpPS"
            }
        }
    
        Function Install-MSI {
             foreach ($Com in $Computers) {
                Enter-PSSession -ComputerName $Com
                invoke-item "c:\tmpPS\ClientInstall.msi"
                Exit-PSSession
            }
        }
    }
    
    # Then the actions
    Process {
        $Computers = Get-Content $ComputerList
        Copy-MSI
        Install-MSI
    }
    

    如果你运行这个,你会找到你正在寻找的信息:

    Get-Help Enter-PSSession
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2019-08-29
      相关资源
      最近更新 更多