Powershell Webjob的写入主机和写入输出问题 [英] Powershell Webjob Write-Host and Write-Output issues

查看:80
本文介绍了Powershell Webjob的写入主机和写入输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些在Webjob中运行的Powershell脚本的日志记录.我在写主机和写输出方面都遇到了问题.

I would like some logging for my Powershell script that I run in a Webjob. I have issues with both Write-Host and Write-Output.

代码

function GetNumber(){
    Write-Host "GetNumber called"
    return 3
}

$number = GetNumber
Write-Host $number
$sum = $number + 1
Write-Host $sum

运行结果:

GetNumber被调用
3
4

GetNumber called
3
4


写主机

在Azure中将其作为Webjob运行,但是导致崩溃,我无法解决:

Running this in Azure as a Webjob however result in a crash I cannot resolve:

[11/03/2017 10:12:30 > c2d2b2: SYS INFO] Status changed to Initializing [11/03/2017 10:12:30 > c2d2b2: SYS INFO] Run script 'LogIssue.ps1' with script host - 'PowerShellScriptHost' [11/03/2017 10:12:30 > c2d2b2: SYS INFO] Status changed to Running [11/03/2017 10:12:31 > c2d2b2: ERR ] Write-Host : The Win32 internal error "The handle is invalid" 0x6 occurred [11/03/2017 10:12:31 > c2d2b2: ERR ] while setting character attributes for the console output buffer. Contact [11/03/2017 10:12:31 > c2d2b2: ERR ] Microsoft Customer Support Services. [11/03/2017 10:12:31 > c2d2b2: ERR ] At D:\local\Temp\jobs\triggered\LogIssue\qysrjmli.z0x\LogIssue.ps1:2 char:5 [11/03/2017 10:12:31 > c2d2b2: ERR ] + Write-Host "GetNumber called" [11/03/2017 10:12:31 > c2d2b2: ERR ] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [11/03/2017 10:12:31 > c2d2b2: ERR ] + CategoryInfo : ResourceUnavailable: (:) [Write-Host], HostExcep [11/03/2017 10:12:31 > c2d2b2: ERR ] tion [11/03/2017 10:12:31 > c2d2b2: ERR ] + FullyQualifiedErrorId : SetConsoleTextAttribute,Microsoft.PowerShell.Com [11/03/2017 10:12:31 > c2d2b2: ERR ] mands.WriteHostCommand [11/03/2017 10:12:31 > c2d2b2: ERR ]

...


写输出

我已经看到了一些使用Write-Output的建议.在某些简单情况下,此方法有效,但会影响函数的返回值.参见https://powershellstation.com/2011/08/26/powershell%E2%80%99s-problem-with-return/

I've seen several suggestions to use Write-Output instead. This works in some simple cases but will affect the return value of functions. See https://powershellstation.com/2011/08/26/powershell%E2%80%99s-problem-with-return/

在代码示例中,如果我们将Write-Host替换为Write-Output,则运行脚本的输出将改为:

In the code example, if we replace Write-Host with Write-Output, the output of the run script will instead be:

GetNumber被调用
3
1

GetNumber called
3
1

注意1!从函数返回的对象是Write-Output和return语句中的某种混合对象.当您回显返回的对象时,它看起来不错,但是对该对象的操作将无法按预期进行.

Note the 1! The object returned from the function is some mixed object from the Write-Output and the return statement. When you echo the returned object it looks OK but operations on the object won't work as expected.

[控制台] :: WriteLine

如果我们改用[console] :: WriteLine

If we use [console]::WriteLine instead

function GetNumber(){
    [console]::WriteLine("GetNumber called")
    return 3
}
$number = GetNumber
[console]::WriteLine("{0}",$number)
$sum = $number + 1
[console]::WriteLine("{0}",$sum)

输出再次正确:

  • 调用了GetNumber
    3
    4
  • GetNumber called
    3
    4

它会在Azure Web作业中输出日志!

And it outputs logs in the Azure web job!!

请注意("{0}",...)格式.由于某些原因,WriteLine($ number)不会创建换行符.

Note the ("{0}",...) format. WriteLine($number) won't create a line-break of some reason.

问题

  1. 我可以配置Web应用程序/Webjob以某种方式支持Write-Host吗? (我的谷歌搜索技能使我失败了)
  2. 在Webjob中是否有更好的(而不是超级复杂的)调试方式
  3. 是否有某种方法可以重写函数或代码一般情况下效果更好的

推荐答案

不确定到底是怎么回事,但是一个建议是尝试在不使用WebJobs的情况下进行隔离,而是使用Kudu控制台(具有PowerShell风格)进行隔离.看 此页面.

Not sure exactly what's going here, but one suggestion is to try to isolate without using WebJobs, and using the Kudu Console instead (which has a PowerShell flavor). See this page.

我还想知道这是否 最近发现的问题与此相关.

I also wonder whether this recently found issue relates to this in some way.


这篇关于Powershell Webjob的写入主机和写入输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆