【发布时间】: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 吗?谢谢。