簡體   English   中英

帶參數的PowerShell調用運算符

[英]PowerShell call operator with argument

抱歉,我編輯我的帖子,因為您沒有所有信息 該腳本的目標是啟動另一個具有提升權限的參數的腳本

我這樣稱呼腳本:

./myscript.ps1 "ScriptName.ps1" "Argument1" "Argument2"

這是我的整個腳本(myscript.ps1):

# ELEVATED SCRIPT
$ScriptName = $args[0]
$Argument1 = $args[1]
$Argument2 = $args[2]

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  # Relaunch as an elevated process:
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}
# Now running elevated so launch the script:
& $ScriptName $Argument1 $Argument2
Pause
# This function allows me to see variables content
function Pause ($Message="Press any key to continue…")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host “”
}

變量在此行為空:

& $ScriptName $Argument1 $Argument2

如果我在腳本頂部進行更改,則此腳本有效:

$ScriptName = "mycommand.exe"
$Argument1 = "something"
$Argument2 = "something_else"

我希望這次足夠清楚。

我已經解決了我的問題。 我停止嘗試以更高的特權執行另一個腳本。 我已將腳本修改為自我提升。 然后,我對我的變量沒有任何問題。

非常感謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM