为什么从 powershell 控制台执行 cmdlet 时不显示 GUI,但在 运行 作为脚本时显示?
Why is the GUI not displayed when executing a cmdlet from powershell console, however displayed when running as a script?
我有一个下面的函数,它显示一个凭证 window 如下。
文件:popup.ps1
(这不是psm。只是一个只有popup()
的脚本)
function popup
{
[CmdletBinding()]
param(
[Parameter (Mandatory=$True)]
[string] $username
)
$credential = Get-Credential -Username $username -Message "Provide Credentials"
$pass = $credential.GetNetworkCredential().password
}
现在,如果此 ps1 来自 PowerShell 控制台并执行 popup()
命令,则不会显示凭据弹出窗口。但是,如果我通过调用脚本包装此函数并执行(即 右键单击 → 运行 使用 PowerShell)我能够看到 GUI 弹出窗口。
文件:wrapper.ps1
. "./popup.ps1"
$uname = "viswa-ksp"
popup -username $uname
谁能解释一下为什么当从 PowerShell 控制台 运行 时不显示 GUI,但是当作为脚本执行时?我在这里遗漏了什么吗?
编辑:
这实际上是我这边的一个错误。同事开发人员评论了采购部分 . <path to the cmdlet source>
作为他针对不同问题的调试/故障排除的一部分,我已经监督了它。但是在心里认为采购仍然存在(因为我知道我已经包括了采购)。
感谢您的宝贵意见。由于超过2人说不可重现,我又去看了一遍剧本,发现了这个问题。
但这是后续问题。我知道对于 cmdlet,您必须获取它。但我想了解,如果您只是 运行 cmdlet 而不采购它,为什么什么也没有发生。没有错误没有弹出窗口。我什至测试了 return 代码。它是空的。
PS C:\Users\kspviswa\Desktop> .\popup.ps1
PS C:\Users\kspviswa\Desktop> $ret = .\popup.ps1
PS C:\Users\kspviswa\Desktop> Write-Host $ret
PS C:\Users\kspviswa\Desktop>
有人能解释一下这种行为吗?
您的函数没有 return 密码,因为您将它分配给一个变量而不是 returning 它。更改此行:
$pass = $credential.GetNetworkCredential().password
进入这个:
$credential.GetNetworkCredential().password
此外,运行创建脚本不会自动调用其中定义的任何函数。函数定义只是使函数在脚本执行的范围内可用。 Dot-sourcing 在当前范围内加载脚本,因此您可以在对脚本进行 dot-sourcing 后在控制台中使用该函数。如果您 运行 脚本在子作用域中执行,那么您需要显式调用子作用域中的函数来调用其功能。参见 about_Scopes
and the dot-sourcing section of about_Scripts
在没有点源的情况下 运行 运行脚本后没有任何返回的原因是,脚本没有抛出任何输出。
将其保存为脚本并 运行 不使用点源
function popup
{
[CmdletBinding()]
param(
[Parameter (Mandatory=$True)]
[string] $username
)
write-host "String Inside function"
$credential = Get-Credential -Username $username -Message "Provide Credentials"
$credential.GetNetworkCredential().password
}
write-host "String outside Function but inside the script"
输出必须是函数外部但脚本内部的字符串
通过点源尝试
PS >. .\script.ps1
PS >String outside Function but inside the script
PS >popup -username USERNAME
PS >String Inside function
如果您不对脚本进行点源,函数将在脚本执行后被定义和删除,因为默认情况下函数的作用域在脚本内部
如果您使用点源代码,它会将函数加载到内存中。
如果你把函数的作用域改成全局的,不用点源也可以使用
function global: popup
{
[CmdletBinding()]
param(
[Parameter (Mandatory=$True)]
[string] $username
)
write-host "String Inside function"
$credential = Get-Credential -Username $username -Message "Provide Credentials"
$credential.GetNetworkCredential().password
}
write-host "String outside Function but inside the script"
您可以 运行 没有点源的脚本
打开一个新的 PowerShell 控制台并试试这个
PS >.\script.ps1
PS >String outside Function but inside the script
PS >popup -username USERNAME
PS >String Inside function
有关范围的更多信息https://technet.microsoft.com/en-us/library/hh847849.aspx
此致,
kvprasoon
我有一个下面的函数,它显示一个凭证 window 如下。
文件:popup.ps1
(这不是psm。只是一个只有popup()
的脚本)
function popup
{
[CmdletBinding()]
param(
[Parameter (Mandatory=$True)]
[string] $username
)
$credential = Get-Credential -Username $username -Message "Provide Credentials"
$pass = $credential.GetNetworkCredential().password
}
现在,如果此 ps1 来自 PowerShell 控制台并执行 popup()
命令,则不会显示凭据弹出窗口。但是,如果我通过调用脚本包装此函数并执行(即 右键单击 → 运行 使用 PowerShell)我能够看到 GUI 弹出窗口。
文件:wrapper.ps1
. "./popup.ps1"
$uname = "viswa-ksp"
popup -username $uname
谁能解释一下为什么当从 PowerShell 控制台 运行 时不显示 GUI,但是当作为脚本执行时?我在这里遗漏了什么吗?
编辑:
这实际上是我这边的一个错误。同事开发人员评论了采购部分 . <path to the cmdlet source>
作为他针对不同问题的调试/故障排除的一部分,我已经监督了它。但是在心里认为采购仍然存在(因为我知道我已经包括了采购)。
感谢您的宝贵意见。由于超过2人说不可重现,我又去看了一遍剧本,发现了这个问题。
但这是后续问题。我知道对于 cmdlet,您必须获取它。但我想了解,如果您只是 运行 cmdlet 而不采购它,为什么什么也没有发生。没有错误没有弹出窗口。我什至测试了 return 代码。它是空的。
PS C:\Users\kspviswa\Desktop> .\popup.ps1
PS C:\Users\kspviswa\Desktop> $ret = .\popup.ps1
PS C:\Users\kspviswa\Desktop> Write-Host $ret
PS C:\Users\kspviswa\Desktop>
有人能解释一下这种行为吗?
您的函数没有 return 密码,因为您将它分配给一个变量而不是 returning 它。更改此行:
$pass = $credential.GetNetworkCredential().password
进入这个:
$credential.GetNetworkCredential().password
此外,运行创建脚本不会自动调用其中定义的任何函数。函数定义只是使函数在脚本执行的范围内可用。 Dot-sourcing 在当前范围内加载脚本,因此您可以在对脚本进行 dot-sourcing 后在控制台中使用该函数。如果您 运行 脚本在子作用域中执行,那么您需要显式调用子作用域中的函数来调用其功能。参见 about_Scopes
and the dot-sourcing section of about_Scripts
在没有点源的情况下 运行 运行脚本后没有任何返回的原因是,脚本没有抛出任何输出。 将其保存为脚本并 运行 不使用点源
function popup
{
[CmdletBinding()]
param(
[Parameter (Mandatory=$True)]
[string] $username
)
write-host "String Inside function"
$credential = Get-Credential -Username $username -Message "Provide Credentials"
$credential.GetNetworkCredential().password
}
write-host "String outside Function but inside the script"
输出必须是函数外部但脚本内部的字符串
通过点源尝试
PS >. .\script.ps1
PS >String outside Function but inside the script
PS >popup -username USERNAME
PS >String Inside function
如果您不对脚本进行点源,函数将在脚本执行后被定义和删除,因为默认情况下函数的作用域在脚本内部
如果您使用点源代码,它会将函数加载到内存中。
如果你把函数的作用域改成全局的,不用点源也可以使用
function global: popup
{
[CmdletBinding()]
param(
[Parameter (Mandatory=$True)]
[string] $username
)
write-host "String Inside function"
$credential = Get-Credential -Username $username -Message "Provide Credentials"
$credential.GetNetworkCredential().password
}
write-host "String outside Function but inside the script"
您可以 运行 没有点源的脚本 打开一个新的 PowerShell 控制台并试试这个
PS >.\script.ps1
PS >String outside Function but inside the script
PS >popup -username USERNAME
PS >String Inside function
有关范围的更多信息https://technet.microsoft.com/en-us/library/hh847849.aspx
此致,
kvprasoon