简体   繁体   English

如果未将/ else命令识别为cmdlet的名称,则会嵌套Powershell

[英]Powershell nested if / else command is not recognized as the name of a cmdlet

I am attempting to write a powershell script that removes VMware Tools off of physical workstations in my environment (don't ask) while ignoring VMs and I am having issues with the nested if / else statements in the " #Execute VMware Tools removal if physical, then write log " section of this code. 我正在尝试编写一个Powershell脚本,以在不考虑虚拟机的情况下从环境中的物理工作站上删除VMware Tools(不要问),并且在“ #Execute VMware Tools物理删除时 ”中嵌套的if / else语句出现问题,然后在此代码中写入“日志 ”部分。 Can anyone with more Powershell experience give me some pointers as to what I may have done wrong? 任何拥有更多Powershell经验的人都可以向我指出我做错了什么吗?

I receive the following error: 我收到以下错误:

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. else:术语'else'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。

I apologize for the amateur formatting, I am still learning Powershell. 我为业余格式化表示歉意,但我仍在学习Powershell。

Thank you for any assistance. 感谢您的协助。

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

I went ahead and made some edits for you so it looks cleaner and fixed your brackets (which was causing the error you are getting). 我继续为您进行了一些编辑,使它看起来更整洁并固定了方括号(这引起了您得到的错误)。 Feel free to ask any questions! 随意问任何问题! As a future reference, the easiest thing to do when you need to check if there is anything wrong with your script you can copy and paste it into PowerShell ISE and it will underline any errors it sees in red. 作为将来的参考,当您需要检查脚本是否有任何错误时,可以将其复制并粘贴到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"
}

Here is the final script I used if anyone ever needs it. 如果有人需要,这是我使用的最终脚本。

Thanks to Cory Etmund and briantist for the pointers, time, and knowledge. 感谢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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM