相关疑难解决方法(0)

通过命令行将变量传递给powershell脚本

我是powershell的新手,并试图自学基础知识.我需要编写一个ps脚本来解析一个文件,这并不算太难.

现在我想更改它以将变量传递给脚本.该变量将是解析字符串.现在,变量将始终为1个单词,而不是一组单词或多个单词.

这看起来很简单,但对我来说却是一个问题.这是我的简单代码:

$a = Read-Host
Write-Host $a
Run Code Online (Sandbox Code Playgroud)

当我从命令行运行脚本时,变量传递不起作用:

.\test.ps1 hello
.\test.ps1 "hello"
.\test.ps1 -a "hello"
.\test.ps1 -a hello
.\test.ps1 -File "hello"
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我已经尝试了许多没有成功的方法,脚本将值输出它.

脚本确实运行,并等待我键入一个值,当我这样做时,它会回显该值.

我只是想让它输出我传入的值,我错过了什么小事?

谢谢.

powershell powershell-ise

52
推荐指数
4
解决办法
29万
查看次数

Powershell 2.0 Param关键字错误

我正在运行Windows 7 RTM.默认情况下安装Powershell 2.0.我正在使用优秀的Windows Powershell ISE来编辑我的脚本.我有以下脚本:

Param($p)
Param($d)
echo $p $d
Run Code Online (Sandbox Code Playgroud)

我将脚本保存为SayItAgain.ps1.当我尝试从交互式shell运行此脚本时,如下所示:

./SayItAgain -p "Hello"
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

At C:\users\cius\Code\powershell\SayItAgain.ps1:2 char:6
+ Param <<<< ($destination)
    + CategoryInfo          : ObjectNotFound: (Param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

这是一个已知问题还是我只是错误使用它?

powershell

42
推荐指数
2
解决办法
5万
查看次数

Powershell非定位,可选参数

我正在尝试创建一个powershell(2.0)脚本,它将接受遵循此基本模式的参数:

.\{script name} [options] PATH
Run Code Online (Sandbox Code Playgroud)

选项是任意数量的可选参数 - 按照'-v'的方式考虑详细.PATH参数将只是最后传递的参数,并且是必需的.可以在没有选项和只有一个参数的情况下调用脚本,并且该参数将被假定为Path.我在设置仅包含可选参数但也非位置的参数列表时遇到问题.

这个快速脚本演示了我遇到的问题:

#param test script
Param(
    $firstArg,
    $secondArg,
    [switch]$thirdArg,
    [Parameter(ValueFromRemainingArguments = $true)]
    $remainingArgs)

write-host "first arg is $firstArg"
write-host "second arg is $secondArg"
write-host "third arg is $thirdArg"
write-host "remaining: $remainingArgs"
Run Code Online (Sandbox Code Playgroud)

当这样调用时:

.\param-test.ps1 firstValue secondValue
Run Code Online (Sandbox Code Playgroud)

脚本输出:

first arg is firstValue
second arg is secondValue
third arg is False
remaining:
Run Code Online (Sandbox Code Playgroud)

试图创建的行为将使两个参数都通过可选参数落在最后的remainingArgs变量中.

这个问题/答案有助于提供一种实现所需行为的方法,但它似乎只有至少有一个强制参数才有效,并且只有在它出现所有其他参数之前.

我可以通过强制firstArg并指定0的位置来演示此行为:

#param test script
Param(
    [Parameter(Mandatory=$true, Position = 0)]
    $firstArg,
    $secondArg,
    [switch]$thirdArg,
    [Parameter(ValueFromRemainingArguments = $true)]
    $remainingArgs)

    write-host …
Run Code Online (Sandbox Code Playgroud)

powershell scripting arguments

17
推荐指数
1
解决办法
3万
查看次数

如何检查调用者是否设置了PowerShell可选参数

我有一个脚本,它接受这些参数:

param (
    [parameter(Mandatory=$true)][ValidateRange(1, [int]::MaxValue)]
    [Int]$startRevision,

    [parameter(Mandatory=$true)][ValidateRange(1, [int]::MaxValue)]
    [Int]$endRevision,

    [parameter(Mandatory=$false)][ValidateRange(1, [int]::MaxValue)]
    [Int]$stepSize = 10,

    [parameter(Mandatory=$false)]
    [String]$applicationToBuild
)
Run Code Online (Sandbox Code Playgroud)

由于最后一个参数是可选的,我想知道参数是否已设置.有没有办法做到这一点?

默认值不正常,因为如果没有设置变量,我不想使用变量.我可以使用默认值"ThisIsNotSet"并检查该值是否等于此字符串,但是有更好的解决方案吗?

powershell

15
推荐指数
1
解决办法
1万
查看次数

Import-PSSession在脚本中失败,在shell中运行

我是Powershell脚本的新手,我正在研究用户管理Powershell脚本,但我遇到了一个奇怪的错误.

以下代码在从shell运行时起作用,但在从脚本运行时不起作用:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
Run Code Online (Sandbox Code Playgroud)

当我Param()在第一行中带有块的脚本中运行它时,它失败并出现以下错误:

Import-PSSession: Cannot bind argument to parameter 'Path' becuase it is an empty string.
Run Code Online (Sandbox Code Playgroud)

Import-PSSession如果我删除我的Param()块,我可以得到工作,但我不知道如何接受我的脚本的命令行参数.如何保留Param()块(或更一般地,接受命令行参数)并仍然能够导入PS会话?

我在试图连接到2010 Exchange服务器的Windows 2008 R2服务器上使用Powershell v2.

更新:

我有一个脚本命名ManageUser.ps1,我从Powershell提示符运行,如

.\ManageUser.ps1 -disableUser -username someuser
Run Code Online (Sandbox Code Playgroud)

该脚本如下所示:

Param(
    [switch]$disableUser,
    [string]$username
)
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
#more stuff past here... …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0 windows-server-2008-r2 exchange-server-2010

5
推荐指数
1
解决办法
3250
查看次数