术语“ Write-PSFMessage”未被识别为cmdlet,函数,脚本文件或可运行程序的名称

时间:2019-10-10 09:19:07

标签: powershell

我正在尝试获取虚拟机名称以进行记录

Set-PSFLoggingProvider -Name logfile -Enabled $true -FilePath 'C:\PDS\++Scripts++\++logs++\replication_logs.log'

$Hosts = "server01","server02"

ForEach ($Server in $Hosts){
    Write-Host 'Primary Server: '$Server
    $VMName             
    Invoke-Command -ComputerName $Server {
    $FailedReplicas = Get-VMReplication | Where{$_.Health -EQ 'Normal'}
        ForEach ($VM in $FailedReplicas){
            $VMName = $VM.Name
            Write-Host 'Virtual Machine: '$VMName 
            try{
                Resume-VMReplication $VMName -Resynchronize
                Write-Host 'Successfully resynched ' $VMName
                Write-PSFMessage -Level Important -Message 'Successfully resynched ' $VMName  -Tag 'Success'
            }
            catch{
                Write-PSFMessage -Level Warning -Message 'Could not resync ' $VMName -Tag 'Failure' -ErrorRecord $_
            }
        }   
    }   
}
pause

运行上面的代码时,出现此错误 术语“ Write-PSFMessage”不被视为cmdlet,函数,脚本文件或可运行程序的名称。检查名称的拼写,或者是否包含路径,请确认路径正确,然后重试。

1 个答案:

答案 0 :(得分:0)

$Hosts = "server01","server02"

ForEach ($Server in $Hosts){
    Write-Host 'Primary Server: '$Server
    $VMName             
    try {
        Invoke-Command -ComputerName $Server -ScriptBlock {
            Import-module PSFramework
            $logPath = "\\x.x.x.123\C$\PDS\++Scripts++\++logs++\replication_logs.log"
            Set-PSFLoggingProvider -Name logfile -Enabled $true -FilePath $logPath
            $FailedReplicas = Get-VMReplication | Where{$_.Health -EQ 'Critical'}
            ForEach ($VM in $FailedReplicas){
                $VMName = $VM.Name
                Write-Host 'Virtual Machine: '$VMName 
                Resume-VMReplication $VMName -Resynchronize 
                Write-Host 'Successfully resynched ' $VMName
                Write-PSFMessage -Level Important -Message 'Successfully resynched'  -Target $VMName  -Tag 'Success'                
            } 
        }

    } catch {
        Write-PSFMessage -Level Warning -Message 'Could not resync ' -Tag 'Failure' -ErrorRecord $_
    }
}
pause