Powershell 嵌套的 if / else 命令未被识别为 cmdlet 的名称
Powershell nested if / else command is not recognized as the name of a cmdlet
我正在尝试编写一个 powershell 脚本,在忽略 VM 的同时从我环境中的物理工作站中删除 VMware Tools(不要问),我在“[=”中的嵌套 if / else 语句遇到问题21=]#Execute VMware Tools removal if physical,然后写入此代码的日志”部分。任何具有更多 Powershell 经验的人都可以就我可能做错的地方给我一些指示吗?
我收到以下错误:
else : 术语 'else' 未被识别为 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"}
我继续为您进行了一些编辑,使它看起来更干净并修复了您的括号(这导致了您遇到的错误)。随意问任何问题!作为未来的参考,当您需要检查脚本是否有任何问题时,最简单的做法是将其复制并粘贴到 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"
}
如果有人需要的话,这是我使用的最后一个脚本。
感谢 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"
}
我正在尝试编写一个 powershell 脚本,在忽略 VM 的同时从我环境中的物理工作站中删除 VMware Tools(不要问),我在“[=”中的嵌套 if / else 语句遇到问题21=]#Execute VMware Tools removal if physical,然后写入此代码的日志”部分。任何具有更多 Powershell 经验的人都可以就我可能做错的地方给我一些指示吗?
我收到以下错误:
else : 术语 'else' 未被识别为 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"}
我继续为您进行了一些编辑,使它看起来更干净并修复了您的括号(这导致了您遇到的错误)。随意问任何问题!作为未来的参考,当您需要检查脚本是否有任何问题时,最简单的做法是将其复制并粘贴到 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"
}
如果有人需要的话,这是我使用的最后一个脚本。
感谢 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"
}