PowerShell和全局功能 [英] PowerShell and global functions

查看:228
本文介绍了PowerShell和全局功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码不工作?根据这篇文章,全球的使用应该是正确的: http://technet.microsoft .com / en-us / library / ff730957.aspx

 全局函数:writeLog {
param ($ logType,$ logString,$ logFile)

$ fileStream = New-Object IO.FileStream $ logFile,'Append','Write','Read'
$ streamWriter = New- Object System.IO.StreamWriter $ fileStream

$ time = get-date -Formathh:mm:ss
$ streamWriter.writeLine([$ {time}] [$ logType ] $ {logString})

$ streamWriter.close()

}

$ temp = {
writeLog -logType INFO-logStringTest-logFiled:\scripts\powershell\logtest.txt
}

Start-Job -ScriptBlock $ temp
get-工作| receive-job -AutoRemoveJob -Wait

这是powershell抛出的异常

 术语'writeLog'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,
验证路径是否正确,然后重试。
+ CategoryInfo:ObjectNotFound:(writeLog:String)[],CommandNotFoundException
+ FullyQualifiedErrorId:CommandNotFoundException
+ PSComputerName:localhost


解决方案

PowerShell作业实际上在单独的PowerShell进程中运行。您可以看到如下所示:

  $ pid 
开始作业{$ pid} | Receive-Job -Wait

其中 $ pid 是当前PowerShell的进程ID。

需要从作业中运行的脚本访问的任何内容,必须在传递给Start-Job的脚本块中定义,即脚本块中定义的函数或作为在Start-Job中使用 -ArgumentList 参数传递给脚本块的参数,或者该脚本可以点出包含所需功能的另一个脚本(或导入模块)。就个人而言,我会将共享函数放在像Utils.psm1这样的模块中,然后像下面这样导入:

  Start-Job {param $ scriptdir)Import-Module $ scriptdir \Utils.psm1; ...} -Arg $ PSScriptRoot 


Why is the following code not working? According to this article the usage of global should be correct: http://technet.microsoft.com/en-us/library/ff730957.aspx

Function global:writeLog {
    param($logType, $logString, $logFile)

    $fileStream = New-Object IO.FileStream $logFile ,'Append','Write','Read'
    $streamWriter = New-Object System.IO.StreamWriter $fileStream

    $time = get-date -Format "hh:mm:ss"
    $streamWriter.writeLine("[${time}][$logType] ${logString}")

    $streamWriter.close()

}

$temp = {
    writeLog -logType "INFO" -logString "Test" -logFile "d:\scripts\powershell\logtest.txt"
}

Start-Job -ScriptBlock $temp 
get-job | receive-job -AutoRemoveJob -Wait

This is the exception that powershell throws

The term 'writeLog' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (writeLog:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : localhost

解决方案

PowerShell jobs actually run in a separate PowerShell process. You can see this like so:

$pid
Start-Job {$pid} | Receive-Job -Wait

Where $pid is the current PowerShell's process id.

Anything that needs to be accessed from the script that runs in the job, must be either defined in the scriptblock passed to Start-Job i.e. function defined in the script block or as parameters passed into the script block using the -ArgumentList parameter on Start-Job or the script can dot source another script (or import a module) that contains the functions it needs. Personally, I would put shared functions in a module like Utils.psm1 and then import like so:

Start-Job {param($scriptdir) Import-Module $scriptdir\Utils.psm1; ...} -Arg $PSScriptRoot

这篇关于PowerShell和全局功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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