PowerShell脚本中的“Session”参数为null或为空

时间:2015-01-08 16:25:46

标签: session powershell powershell-remoting

我是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

1 个答案:

答案 0 :(得分:0)

尽量不要使用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