2017-08-03 196 views
0

我正在嘗試編寫一個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"} 
+3

你缺少一個開放的'if'語句(前一個被關閉)。嘗試使用更標準的格式,它會跳出你的身體。 – briantist

+0

嗨briantist,謝謝你看看。說實話,我不知道什麼是更標準的格式化,因爲現在我所知道的所有東西都是自學的(我在秋季參加PS課)。當您說第一個* if *已關閉時,是否意味着第二個* if *在*#執行VMware Tools刪除(如果物理),然後寫入日誌*節?謝謝。 – DieGeist

回答

0

我繼續前進,併爲您做了一些編輯,使它看起來更乾淨,並修復了您的括號(這會導致您得到的錯誤)。隨意問任何問題!作爲未來的參考資料,當您需要檢查腳本是否有任何問題時,您可以將其複製並粘貼到PowerShell ISE中,這將是最容易的事情,它會強調它以紅色顯示的任何錯誤。

#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 
$Computer = $env:COMPUTERNAME 

switch ($ComputerSystemInfo.Model) { 
    # Check for VMware Machine Type 
    "VMware Virtual Platform" { 
    $Global:MachineType = "VM" 
    } 
    # Otherwise it is a physical Box 
    default { 
    $Global: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 | % {$Global:keypath = $_.pschildname 
    $Global:key = Get-Itemproperty $regpath\$keypath} 
} 

if($key.DisplayName -match "VMware Tools"){ 
    $VMwareToolsGUID = $keypath 
    MsiExec.exe /x $VMwareToolsGUID /qn /norestart -wait 
    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" 
} 
+0

我注意到的一件事是,你有$ Env:COMPUTERNAME的行。該行正在獲取計算機名稱,但您沒有在腳本的其他任何地方使用它。所以真的可以刪除,除非你打算把這些信息放在某個地方! – cet51

+0

感謝您的所有協助人員。我現在對正確的格式有了更好的理解。我對列出的答案進行了一些其他更改,以使其在我的環境中正常工作,但這些只是重新排序命令。我看看我是否可以將它發佈給其他可能需要相同幫助的人。 – DieGeist

+0

嗨Corey,我使用的行是* $ ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ env:COMPUTERNAME -ErrorAction Stop *。在這種情況下* $ env:COMPUTERNAME *正在給我提供* model *的系統信息。這用於下一行* switch($ ComputerSystemInfo.Model){*。 – DieGeist

0

這是我使用的最終腳本,如果有人需要它。

感謝Cory Etmund和briantist的指針,時間和知識。

#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" { 
    $Global:MachineType = "VM" 
} 
    # Otherwise it is a physical Box 
    default { 
    $Global:MachineType = "Physical" 
    } 
} 

#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" 
    exit 
} 

#Execute VMware Tools removal if physical, then write log 
if($MachineType -eq "Physical") { 
    $regpath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\uninstall" 
    Get-childItem $regpath | % {$Global:keypath = $_.pschildname 
    $Global:key = Get-Itemproperty $regpath\$keypath} 
} 

if($key.DisplayName -match "VMware Tools"){ 
    $VMwareToolsGUID = $keypath 
    MsiExec.exe /x $VMwareToolsGUID /qn /norestart -wait 
    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" 
} 
相關問題