我正在尝试编写一个 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"}