0

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

2 回答 2

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"
}
于 2017-08-03T19:16:53.237 回答
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"
}
于 2017-08-03T15:53:51.847 回答