我正在尝试编写一个powershell脚本,可以在忽略虚拟机时从我的环境中的物理工作站中删除VMware Tools(不要问),而且我正在与“#执行VMware Tools删除(如果物理),然后写入此代码的日志”部分中嵌套的if/else语句相关的问题。有更多Powershell经验的人能给我一些关于我可能做错了什么的指示吗?Powershell嵌套的if/else命令不被识别为cmdlet的名称
我收到以下错误:
其他:术语“别人”不被识别为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
嗨briantist,谢谢你看看。说实话,我不知道什么是更标准的格式化,因为现在我所知道的所有东西都是自学的(我在秋季参加PS课)。当您说第一个* if *已关闭时,是否意味着第二个* if *在*#执行VMware Tools删除(如果物理),然后写入日志*节?谢谢。 – DieGeist