2017-10-13 53 views
0

我們有設置WinRM連接的類型類型的服務器安全和正常。Powershell:爲SSL和NOSSL連接構建動態調用命令

我需要使用或不使用-usessl開關建立遠程PowerShell連接。

我想避免有兩個相同的腳本塊,唯一的區別是有或沒有-usessl開關。

我嘗試了使用參數splatting但無法處理-usessl開關,也嘗試過使用invoke-expression,但是這打破了檢索數據的能力;來自遠程作業的散列值。

有什麼建議嗎?

if (test-wsman -ErrorAction SilentlyContinue -cn "$($Cmpname.name).sample.com" –UseSSL) { 
Write-host $Cmpname.name,WSMan Connected SSL 
$strWSMAN = "SSL" 
$strRemoteHash = invoke-command -cn "$($Cmpname.name).sample.com" -usessl -scriptblock { 
     Write-host "calculating Hash values" 
     $strLocalhash = Get-ChildItem -Path "c:\Windows\ccmcache" -Filter "Windows10.0-KB4041691-x64.cab" -Recurse -ErrorAction SilentlyContinue -Force | Get-FileHash -Algorithm sha1 

    New-Object pscustomobject -Property @{ 
    RemotePath = $strLocalhash.path 
    RemoteHash = $strLocalhash.hash 
    } 
    } 

} else { 
    $strWSMAN = "NoSSL" 
    $strRemoteHash = invoke-command -cn "$($Cmpname.name).sample.com" -scriptblock { 
    Write-host "calculating Hash values" 
    $strLocalhash = Get-ChildItem -Path "c:\Windows\ccmcache" -Filter "Windows10.0-KB4041691-x64.cab" -Recurse -ErrorAction SilentlyContinue -Force | Get-FileHash -Algorithm sha1 

    New-Object pscustomobject -Property @{ 
    RemotePath = $strLocalhash.path 
    RemoteHash = $strLocalhash.hash 
    } 
    } 
    } 
+1

-UseSSL是一個開關參數。您可以根據是否要啓用或不使用$ true或$ false作爲參數值來使用splatting。 – mjolinor

回答

0

由於mjolinor said,你需要爲一個開關參數提供了一個布爾值潑灑時:

$ParameterValues = @{ 
    ComputerName = "$($Cmpname.name).sample.com" 
    UseSSL = [bool](Test-WSMan -ErrorAction SilentlyContinue -ComputerName "$($Cmpname.name).sample.com" –UseSSL) 
    ScriptBlock = { 
     Write-host "calculating Hash values" 
     $strLocalhash = Get-ChildItem -Path "c:\Windows\ccmcache" -Filter "Windows10.0-KB4041691-x64.cab" -Recurse -ErrorAction SilentlyContinue -Force | Get-FileHash -Algorithm sha1 

     New-Object pscustomobject -Property @{ 
      RemotePath = $strLocalhash.path 
      RemoteHash = $strLocalhash.hash 
     } 
    } 
} 
Invoke-Command @ParameterValues