2017-08-03 40 views
0

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

你缺少一个开放的'if'语句(前一个被关闭)。尝试使用更标准的格式,它会跳出你的身体。 – briantist

+0

嗨briantist,谢谢你看看。说实话,我不知道什么是更标准的格式化,因为现在我所知道的所有东西都是自学的(我在秋季参加PS课)。当您说第一个* if *已关闭时,是否意味着第二个* if *在*#执行VMware Tools删除(如果物理),然后写入日志*节?谢谢。 – DieGeist

回答

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" 
} 
+0

我注意到的一件事是,你有$ Env:COMPUTERNAME的行。该行正在获取计算机名称,但您没有在脚本的其他任何地方使用它。所以真的可以删除,除非你打算把这些信息放在某个地方! – cet51

+0

感谢您的所有协助人员。我现在对正确的格式有了更好的理解。我对列出的答案进行了一些其他更改,以使其在我的环境中正常工作,但这些只是重新排序命令。我看看我是否可以将它发布给其他可能需要相同帮助的人。 – DieGeist

+0

嗨Corey,我使用的行是* $ ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ env:COMPUTERNAME -ErrorAction Stop *。在这种情况下* $ env:COMPUTERNAME *正在给我提供* model *的系统信息。这用于下一行* switch($ ComputerSystemInfo.Model){*。 – DieGeist

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" 
} 
相关问题