我正在嘗試編寫一個powershell腳本,可以在忽略虛擬機時從我的環境中的物理工作站中刪除VMware Tools(不要問),而且我正在與「#執行VMware Tools刪除(如果物理),然後寫入此代碼的日誌」部分中嵌套的if/else語句相關的問題。有更多Powershell經驗的人能給我一些關於我可能做錯了什麼的指示嗎?Powershell嵌套的if/else命令不被識別爲cmdlet的名稱
我收到以下錯誤:
其他:術語「別人」不被識別爲cmdlet,函數,腳本文件或可操作的程序的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後再次嘗試 。
我爲業餘格式道歉,我還在學習Powershell。
謝謝你的任何援助。
#Create log path
$path = "D:\log\folder\location\"
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
}
#Gather required host info
$ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -
ComputerName $env:COMPUTERNAME -ErrorAction Stop
switch ($ComputerSystemInfo.Model) {
# Check for VMware Machine Type
"VMware Virtual Platform" {
$MachineType = "VM"
}
# Otherwise it is a physical Box
default {
$MachineType = "Physical"
}
}
#Execute VMware Tools removal if physical, then write log
if($MachineType -eq "Physical") {
$regpath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\uninstall"
Get-childItem $regpath | % {$keypath = $_.pschildname
$key = Get-Itemproperty $regpath\$keypath}
if($key.DisplayName -match "VMware Tools")
{$VMwareToolsGUID = $keypath} MsiExec.exe /x $VMwareToolsGUID /qn /norestart
{Write-output "VMware Tools Uninstalled" | Out-File -Encoding ascii -filepath "D:\log\folder\location\VMware_Uninstalled.txt"}
else
{Write-output "VMware Tools Not Present" | Out-File -Encoding ascii -filepath "D:\log\folder\location\VMware_Not_Present.txt"}
}
#Write output log if VM
if($MachineType -eq "VM")
{Write-Output "Machine is virtual" | Out-File -Encoding ascii -filePath
"D:\log\folder\location\Virtual_Machine.txt"}
else
{Write-Output "Error" | Out-File -Encoding ascii -FilePath
"D:\log\folder\location\Error.txt"}
你缺少一個開放的'if'語句(前一個被關閉)。嘗試使用更標準的格式,它會跳出你的身體。 – briantist
嗨briantist,謝謝你看看。說實話,我不知道什麼是更標準的格式化,因爲現在我所知道的所有東西都是自學的(我在秋季參加PS課)。當您說第一個* if *已關閉時,是否意味着第二個* if *在*#執行VMware Tools刪除(如果物理),然後寫入日誌*節?謝謝。 – DieGeist