【问题标题】:Powershell nested if / else command is not recognized as the name of a cmdletPowershell 嵌套的 if / else 命令未被识别为 cmdlet 的名称
【发布时间】:2017-08-04 03:16:53
【问题描述】:

我正在尝试编写一个 powershell 脚本,在忽略虚拟机的同时从我的环境中的物理工作站中删除 VMware Tools(不要问),并且我遇到了“#”中嵌套的 if / else 语句的问题如果是物理的,则执行 VMware Tools 删除,然后写入此代码的日志”部分。任何有更多 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"}

【问题讨论】:

  • 您缺少一个开头的 if 声明(前一个已关闭)。尝试使用更标准的格式,它会立即跳出来。
  • 您好,briantist,感谢您的浏览。老实说,我不知道更标准的格式是什么,因为我现在所知道的一切都是自学的(我在秋季上 PS 课程)。当您说第一个 if 已关闭时,您是指 #Execute VMware Tools remove if physical, then write log 部分中的第二个 if 吗?谢谢。

标签: powershell vmware-tools


【解决方案1】:

我继续为您进行了一些编辑,使其看起来更干净并修复了您的括号(这导致了您遇到的错误)。随意问任何问题!作为将来的参考,当您需要检查您的脚本是否有任何问题时,最简单的做法是将其复制并粘贴到 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"
}

【讨论】:

  • 我注意到的一件事是你有 $Env:COMPUTERNAME 的那一行。该行正在获取计算机名称,但您没有在脚本中的其他任何地方使用它。因此,除非您打算将这些信息放在某个地方,否则真的可以删除该行!
  • 感谢您的所有帮助。我现在对正确的格式有了更好的理解。我对列出的答案进行了一些额外的更改,以使其在我的环境中工作,但这些只是重新排序命令。我会看看我是否可以将它发布给可能需要相同帮助的其他人。
  • 嗨,Corey,我使用的线路是 $ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $env:COMPUTERNAME -ErrorAction Stop。在这种情况下,$env:COMPUTERNAME 为我提供了为我提供 model 的系统信息。这用于下一行 switch ($ComputerSystemInfo.Model) {.
  • 啊,我想知道这是怎么回事。当我复制并粘贴您的代码时,它会将它们放在两个单独的行中,而我没有注意到它。很高兴我能帮上忙!
【解决方案2】:

如果有人需要,这是我使用的最终脚本。

感谢 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"
}

【讨论】:

    猜你喜欢
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多