如何将参数参数从ps.1参数传递给同名函数?

时间:2018-12-03 15:40:55

标签: powershell powershell-v3.0

我有一个诊断性Powershell脚本,希望服务器管理员将其与以下用户流一起使用:

  • 系统管理员将脚本粘贴到“ RDP /远程桌面”窗口中。立即执行
  • 假设脚本策略允许,Sysadmin将从命令行运行脚本

在以下情况下,我还希望有一个诊断选项来运行:

  • 系统管理员运行带有参数(-debug)的函数
  • 系统管理员使用功能运行脚本

这是简化示例

Inspect-ProductionHost.ps1

!SESSION 2018-11-30 14:41:07.845 -----------------------------------------------
eclipse.buildId=4.9.0.I20180906-0745
java.version=11.0.1
java.vendor=Oracle Corporation
BootLoader constants: OS=macosx, ARCH=x86_64, WS=cocoa, NL=en_GB
Framework arguments:  -product org.eclipse.epp.package.java.product -keyring /Users/izziharper/.eclipse_keyring
Command-line arguments:  -os macosx -ws cocoa -arch x86_64 -product org.eclipse.epp.package.java.product -keyring /Users/izziharper/.eclipse_keyring

!ENTRY org.eclipse.mylyn.bugzilla.core 4 0 2018-11-30 14:41:08.591
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.mylyn.bugzilla.core [278]
  Unresolved requirement: Require-Bundle: org.apache.xmlrpc
    -> Bundle-SymbolicName: org.apache.xmlrpc; bundle-version="3.0.0.v20100427-1100"
       org.apache.xmlrpc [68]
         Unresolved requirement: Import-Package: javax.xml.bind
  Unresolved requirement: Require-Bundle: org.eclipse.mylyn.commons.xmlrpc; bundle-version="[3.8.0,4.0.0)"
    -> Bundle-SymbolicName: org.eclipse.mylyn.commons.xmlrpc; bundle-version="3.24.2.v20180904-2231"; singleton:="true"
       org.eclipse.mylyn.commons.xmlrpc [295]
         Unresolved requirement: Require-Bundle: org.apache.xmlrpc; bundle-version="[3.0.0,4.0.0)"
           -> Bundle-SymbolicName: org.apache.xmlrpc; bundle-version="3.0.0.v20100427-1100"

问题在于,当我从命令行function Inspect-ProductionHost() { param ([switch]$DoDebug) if($DoDebug -eq $True) { Get-DebugData Write-output $DebugData } write-output "Testing Server" TestHost-Configuration1 } cls #clear the output incase this is pasted in. Simplify diagnostics Inspect-ProductionHost($DoDebug) 执行此操作时,参数不会流到函数。

替代解释

我想通过以下方式运行脚本:

  1. 将代码粘贴到RDP .\Inspect-ProductionHost -DoDebug中,它无需$ DebugData即可运行
  2. 粘贴后,输入Inspect-ProductionHost

-或-

  1. 从命令行Inspect-ProductionHost -DoDebug运行脚本
  2. 从命令行.\Inspect-ProductionHost运行脚本

这在Powershell中可以实现吗?

1 个答案:

答案 0 :(得分:0)

这个...

Inspect-ProductionHost($ DoDebug)

...不是您要使用的开关。您以后显示的选项是。

。\ Inspect-ProductionHost -DoDebug

此外,对于开关,他们以这种方式使用它...

if($ DoDebug -eq $ True)

...并不是真正需要的。可能就是这个...

if($ DoDebug)

至于... “•Sysadmin将脚本粘贴到RDP / Remote Desktop窗口中。它立即执行”。

除非将其粘贴到PowerShell ConsoleHost实例中,否则必须在登录到RDP目标后启动该实例,否则不会发生。

通过这种努力,您只需将原始代码粘贴到ConsoleHost中,并在按下Enter键时运行它。粘贴代码,不运行代码。仍然需要交互式键盘操作。

您确实应该将脚本复制到RDP位置,并要求管理员从PowerShell主机运行该脚本,而不是复制粘贴。 这样,您可以使用您要使用的调试开关运行该调试开关。

在此复制粘贴工作中(仍然需要交互步骤),要使调试发生,您应该使参数成为具有布尔选项的必需项。

所以,这是您的第二个选择

3。从命令行运行脚本。\ Inspect-ProductionHost 4.从命令行运行脚本。\ Inspect-ProductionHost -DoDebug

...这是最谨慎的方法。

以其他方式,除非登录到RDP目标时具有PowerShell控制台主机自动启动,否则必须手动启动它并手动运行代码。

另一种选择是将脚本复制到RDP目标,使用登录脚本将其触发,并让您脚本提示进行调试运行(即布尔响应)。

function Inspect-ProductionHost()
{ 
    param ([switch]$DoDebug)

    if($DoDebug -eq $True)
    {
        Get-DebugData 
        Write-output $DebugData
    }

    write-output "Testing Server"
    Test-Connection -ComputerName $env:COMPUTERNAME
}

Inspect-ProductionHost
Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        



Inspect-ProductionHost($DoDebug)
Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        



Inspect-ProductionHost -DoDebug
Get-DebugData : The term 'Get-DebugData' 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.
At line:7 char:9
+         Get-DebugData
+         ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DebugData:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        








function Inspect-ProductionHost()
{ 
    param ([switch]$DoDebug)

    if($DoDebug)
    {
        Get-DebugData 
        Write-output $DebugData
    }

    write-output "Testing Server"
    Test-Connection -ComputerName $env:COMPUTERNAME
}

Inspect-ProductionHost
Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        



Inspect-ProductionHost($DoDebug)
Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        



Inspect-ProductionHost -DoDebug
Get-DebugData : The term 'Get-DebugData' 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.
At line:7 char:5
+     Get-DebugData
+     ~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Get-DebugData:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0....       fe80::b812:f23d:94b3:33e9%4              32       0        

如果您确实想执行此复制粘贴操作,这又是一件很奇怪的事情,那么这不必是一个函数。只需将以下内容复制并粘贴到consolehost中即可。

param 
(
    [Parameter(Mandatory=$true)]
    [ValidateSet('True','False')] 
    [string]$DoDebug
)

if($DoDebug -eq 'True')
{
    Get-DebugData 
    Write-output $DebugData
}

write-output "Testing Server"
Test-Connection -ComputerName $env:COMPUTERNAME
cmdlet  at command pipeline position 1
Supply values for the following parameters:
DoDebug: false
Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        



param 
(
    [Parameter(Mandatory=$true)]
    [ValidateSet('True','False')] 
    [string]$DoDebug
)

if($DoDebug -eq 'True')
{
    Get-DebugData 
    Write-output $DebugData
}

write-output "Testing Server"
Test-Connection -ComputerName $env:COMPUTERNAME
cmdlet  at command pipeline position 1
Supply values for the following parameters:
DoDebug: true
Get-DebugData : The term 'Get-DebugData' 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.
At line:10 char:5
+     Get-DebugData
+     ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DebugData:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Testing Server

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
WS01          WS01            10.0.0...       fe80::b812:f23d:94b3:33e9%4              32       0        
相关问题