請參考以下範例簽署的 PowerShell 腳本,以用於執行遠端自訂腳本回應動作。
請遵循範例腳本中的格式,以避免輸出編碼和檢索腳本參數時出現問題。
# Directory structure is as follows: # C:\example\ # |----your_signed_script.ps1 # |----tm_script_parameter.txt # Command line example for launching PowerShell: # PowerShell.exe -ExecutionPolicy RemoteSigned -File "C:\example\your_signed_script.ps1" "C:\example\tm_script_parameter.txt" # Follow a simple format for your signed script to handle script parameters: # Use utf8 encoding: $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = [Text.UTF8Encoding]::UTF8 # Read raw parameter string from $args[0]: $ArgString = Get-Content -Path $args[0] -Encoding UTF8 # Split raw string: $InputArgs = $ArgString -split '\s+(?=(?:[^"]*"[^"]*")*[^"]*$)' | Where-Object { $_ -ne '' } | ForEach-Object { $_.Trim('"') } function EntryPoint { ##################################################### # Your script content goes in the EntryPoint function. # Specify function parameters: [CmdletBinding()] param ( [Parameter()][string]$Id, [Parameter()][string]$Name ) Write-Host "ID: $Id, Name: $Name" ##################################################### } # Change the parameter string array to Params variables: $NamedParams = @{} $PositionalParams = @() for ($i = 0; $i -lt $InputArgs.Length; $i++) { $Arg = $InputArgs[$i] if ($Arg.StartsWith("-")) { # Find named parameter: $ParamName = $Arg $ParamValue = $InputArgs[$i + 1] $NamedParams[$ParamName] = $ParamValue $i++ } else { # Find positional parameter: $PositionalParams += $Arg } } # Call EntryPoint function with the Params variables: EntryPoint @NamedParams @PositionalParams