Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PowerShell中的cmdlet捕获消息_Powershell - Fatal编程技术网

通过PowerShell中的cmdlet捕获消息

通过PowerShell中的cmdlet捕获消息,powershell,Powershell,我正在运行下面的脚本。如果“复制项目”命令成功完成,则不会显示任何消息,例如复制了多少文件。我如何捕捉到这一点 注意:我还需要捕获脚本正确执行的错误消息 $LogFile = "P:\users\Logname.log" $msg = Copy-Item P:\Bkp_20130610\* P:\users -force -recurse -ErrorAction SilentlyContinue if (-not $?) { msg1 = $Error[

我正在运行下面的脚本。如果“复制项目”命令成功完成,则不会显示任何消息,例如复制了多少文件。我如何捕捉到这一点

注意:我还需要捕获脚本正确执行的错误消息

    $LogFile = "P:\users\Logname.log"
    $msg = Copy-Item P:\Bkp_20130610\* P:\users -force -recurse -ErrorAction SilentlyContinue
    if (-not $?)
   {
     msg1 = $Error[0].Exception.Message
      Write-Host "Encountered error. Error Message is $msg1."
     exit
   }

   $msg > $LogFile
   Write-Host "Hello"

您可以将
-Verbose
开关与
Copy Item
cmdlet一起使用:

$msg=Copy-Item P:\Bkp_20130610\* P:\users -force -recurse -ErrorAction SilentlyContinue -Verbose

您可以将
-Verbose
开关与
Copy Item
cmdlet一起使用:

$msg=Copy-Item P:\Bkp_20130610\* P:\users -force -recurse -ErrorAction SilentlyContinue -Verbose

您可以通过这种方式获得复制文件的列表

$files = copy-item -path $from -destination $to -passthru 
通过管道将其连接到
|?{-not$\ psiscontainer}
如果您正在复制文件夹,并且不希望它们出现在计数中

然后使用

 $files.count

您可以通过这种方式获得复制文件的列表

$files = copy-item -path $from -destination $to -passthru 
通过管道将其连接到
|?{-not$\ psiscontainer}
如果您正在复制文件夹,并且不希望它们出现在计数中

然后使用

 $files.count

嗨,拉维,我已经试过了。它显示在屏幕上,而不是传递给变量$msgYea!我误解了你的问题。你也可以使用Tee Object cmdlet。嗨,Ravi,我已经按照你提到的那样尝试过了。它显示在屏幕上,而不是传递给变量$msgYea!我误解了你的问题。您也可以使用Tee-Object cmdlet。@AvinashGanesh很乐意提供帮助@阿维纳什甘尼什很乐意帮忙!