gpt4 book ai didi

function - 基本 PowerShell 功能无输出

转载 作者:行者123 更新时间:2023-12-03 00:06:29 24 4
gpt4 key购买 nike

我刚开始使用 PowerShell 已经有一段时间了,到今天我可以说我已经完成了很多日常任务。在学校的每一天都是新的一天,现在,当我开始使用函数时,我又一次绊倒了……
这是我的问题:

即使是从许多初学者教程中获取的最简单的功能,它也不能在我的工作 PC、我的家用 PC 或我工作的管理服务器上工作。

例如:

function MyDate {
Get-Date
}

将名为 MyDate.ps1 的 PS1 文件保存到本地文件夹,然后像这样从 PowerShell 提示符运行这个小东西
PS C:\PS> C:\PS\MyDate.ps1

这就是它在 PS-Functions-for-Dummies 教程中的确切解释。
就在教程中,输出应该显示日期。在我的情况下,什么都没有。没有错误,没有红色 PS 特定的信息,什么都没有!
如果我像这样在完整脚本中使用该函数:
function MyDate {
Get-Date
}
MyDate

PS C:\PS> C:\PS\MyDate.ps1

Montag, 1. Oktober 2018 19:28:32

好吧,然后我得到日期输出。
想知道为什么不直接从提示中调用脚本中的函数,因为应该使用函数?
再一次,在许多这些非常简单的例子中,它应该可以工作..
那我哪里错了?

编辑:不确定这是否是为问题添加更多更新的正确政策?

我可能会分享一个更具体的脚本示例,以及我想如何将其“转换”为函数。简短的例子是:
$myServers = Get-Content D:\Servers\Windows2016.txt
foreach ($myServer in $myServers) {
Get-Printer -ComputerName $myServer |
Where-Object {$_.DeviceType -eq "Printer"} |
Select Name, PortName
}

对于其他支持团队,它必须尽可能简单。所以我的想法是围绕这几行“包装”一个函数,以便另一个人可以像这样执行它
C:\Scripts\Our-Printers -File D:\Servers\Windows2016.txt
Our-Printers那当然是我的函数名。

我已经尝试过了,但放弃了这些东西,因为它在第一次尝试时不起作用,这就是为什么我想从非常基本的东西开始,而目前我似乎不明白。但顺便说一句,在脚本中我的函数已经工作了,只是它们不需要以示例的方式输入。这意味着我在一个大脚本中定义了函数,并且我可以根据用户输入更改订单甚至跳过某些函数......

最佳答案

在脚本中定义函数意味着您编写可以通过符号名称调用的代码块。但是,该函数不会通过运行脚本自动调用。你仍然需要真正做到这一点。

PS C:\> Get-Content .\a.ps1function GetDate {    Get-Date}PS C:\> .\a.ps1PS C:\> Get-Content .\b.ps1function GetDate {    Get-Date}GetDate                  # ← actually invoke the functionPS C:\> .\b.ps1Montag, 1. Oktober 2018 19:50:32

Only (script-)global scope code in a script is executed automatically when the script is invoked.

Depending on the purpose of your script this may or may not be what you want. For instance, if you intend to just run the script from some external script or command to have it do something it makes sense to automatically invoke the function when the script is executed. If on the other hand you intend to use the script as a kind of library, storing different functions that you want to be able to invoke separately, you'd omit function invocations from the script. Then you can dot-source the script as Mathias described and invoke the function(s) you need.

PS C:\> Get-Content .\a.ps1function GetDate {    Get-Date}PS C:\> . .\a.ps1    # ← load the function definition into the current scopePS C:\> GetDate      # ← actually invoke the functionMontag, 1. Oktober 2018 19:50:32

Edit: Your ultimate goal could be achieved either way. There are files (called profiles) that PowerShell sources automatically when launched. You could define a function and put that in the appropriate profile (per user or system-wide). After restarting PowerShell people will then be able to invoke the function.

Alternatively you could put the code in a script (without wrapping it in a function), and put that script someplace where the intended users can execute it.

Which of these two approaches is preferable for your situation depends on a number of factors. Do you have an domain or a workgroup? In case of a domain, can you make changes to the AD? Do you have a shared location where you could put files? Do you have a software deployment system? And so on.

Another important question is: will the users invoke the code just from PowerShell or also from elsewhere (e.g. CMD). In case of the latter one would normally prefer a script over a function in a profile.

Either way you'd parametrize your script or function like this:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$File
)

$myServers = Get-Content $File
foreach ($myServer in $myServers) {
Get-Printer -ComputerName $myServer |
Where-Object {$_.DeviceType -eq "Printer"} |
Select-Object Name, PortName
}

参数定义分别属于脚本或函数的开头。

添加 comment-based help ,以便人们可以运行 Get-Help Our-PrintersOur-Printers -?看看他们应该如何使用它,例如

<#
.SYNOPSIS
List printers.

.DESCRIPTION
List printers. The computers to check are read from a file.

.PARAMETER File
A file with a list of computer names (one per line).
#>

使用 parameter validation用于强制执行有效输入,例如
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path -LiteralPath $_})]
[string]$File
)

关于function - 基本 PowerShell 功能无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com