我在高等教育部門作業,我們需要 100 多個教室中的每一個計算機在桌面上都有一個指向房間攝像頭 IP 地址的 URL 快捷方式。我正在嘗試使用 PowerShell 腳本自動執行此操作。我創建了一個包含表格的腳本,查找計算機名稱,在表格中查找相關的 IP 地址,并在桌面上創建 URL 快捷方式。不幸的是,每當我嘗試將 $IP 變數傳遞給 $urlShortcut.TargetPath 時都會出錯。我希望這是一個簡單的修復?提前致謝!
這是我的腳本的通用版本。例如,如果我替換$urlShortcut.TargetPath = $IP
為$urlShortcut.TargetPath = "https://www.google.com"
,它會創建 Google 的快捷方式。
#Create table
$table = @{}
$table.Add('Room 1,'1.1.1.1')
$table.Add('Room 2','1.1.1.2')
#Determine computer name
$CPUName = $env:computername
#Determine IP address based on room
$IP = $table[$CPUName]
#Display IP Address
write-output $IP
#Create shortcut on desktop
$wshShell = New-Object -ComObject "WScript.Shell"
$urlShortcut = $wshShell.CreateShortcut(
(Join-Path $wshShell.SpecialFolders.Item("AllUsersDesktop") "Camera Control.url")
)
$urlShortcut.TargetPath = $IP
$urlShortcut.Save()
uj5u.com熱心網友回復:
Mathias R. Jessen提供了關鍵指標:
- 考慮到您要打開一個URL
"http://$IP/"
,而不是僅$IP
在分配給時使用。$urlShortcut.TargetPath
快捷方式檔案在其.TargetPath
屬性中ShellExecuteEx
支持的命令形式是WinAPI 函式所識別的形式,PowerShell 的Start-Process
cmdlet 和cmd.exe
的內部start
命令都包含這些函式。
因此,您可以使用;測驗快捷方式檔案的命令。Start-Process
例如:
# !! WRONG
# Without a protocol specifier such as http:, the URL isn't recognized.
Start-Process example.org
# OK
Start-Process https://example.org
但是,與快捷方式檔案和's不同的是, [1]Start-Process
不做的是擴展- 樣式的環境變數參考,例如.cmd.exe
start command
cmd.exe
%windir%
因此,從PowerShell 中完整模擬快捷方式檔案命令列的功能需要cmd.exe
start
cmd /c
通過;呼叫內部命令。例如,測驗命令列notepad.exe %windir%\win.ini
:
cmd /c 'start "" notepad.exe %windir%\win.ini'
注意:""
命令列前面的引數系結到start
用于指定新行程的視窗標題的引數(僅適用于呼叫控制臺應用程式)。系結此引數允許您使用"..."
-enclosed 可執行路徑作為命令列的一部分進行測驗。
[1] 由于它所構建的 .NET API 的行為,System.Diagnostics.Process
.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/506965.html