简体   繁体   English

将参数传递给Powershell

[英]Pass argument into Powershell

I have a powershell script that completes some tasks in Active Directory and MS Exchange. 我有一个powershell脚本,可以完成Active Directory和MS Exchange中的一些任务。 I need to pass in the Active Directory username from our call logging system. 我需要从我们的呼叫记录系统传入Active Directory用户名。 Having the Call log system pass the argument is simple. 让Call日志系统传递参数很简单。

The problem i am facing is having powershell read the argument into a variable. 我面临的问题是让PowerShell将参数读入变量。

I will provide a proof of concept example. 我将提供一个概念验证示例。

Here is a sample command passing the argument into powershell. 这是一个将参数传递给powershell的示例命令。

C:\Users\UserName\Desktop\Test.ps1 -ADusername "Hello World"

Here is the sample script: 以下是示例脚本:

Param([string]$adusername)
$adusername
pause

I am Expecting the following output: 我期待以下输出:

Hello World
Press Enter to continue...:

This is the actual output: 这是实际输出:

Press Enter to continue...:

Just wrapping my head around this core concept will help me immensely. 围绕这个核心概念围绕我的头脑将对我有很大的帮助。 I was unable to find any examples or tutorials that worked when applied to my scenario. 我无法找到适用于我的场景时有用的任何示例或教程。 I apologize if this is a duplicate post, couldnt find anything on this site as well. 我很抱歉,如果这是一个重复的帖子,也无法在这个网站上找到任何东西。

EDIT: per request, this is my full script: http://pastebin.com/ktjpLQek 编辑:每个请求,这是我的完整脚本: http//pastebin.com/ktjpLQek

I think you will have much better luck if you avoid trying to use params and call the script exactly that way. 如果你避免尝试使用params并以这种方式调用脚本,我想你会有更好的运气。

It is possible, but paramaters work better if you either inline the scriptfile like: 这是可能的,但如果您将脚本文件内联如下,则参数更好地工作:

. .\scriptFile.ps1
function "Hello World"

Staying closer to what you are doing however, you should be using $args and calling PowerShell (the exe directly) 然而,更接近你正在做的事情,你应该使用$ args 调用PowerShell(exe直接)

If you call your scriptfile like: (I used the runbox) 如果你调用你的脚本文件:(我使用了runbox)

powershell c:\Path\Folder\Script.ps1 "Hello World!"

and then replace your Param([string]$adusername) with: 然后将您的Param([string]$adusername)替换为:

$adUserName = $args[0]
write-host $adUserName

Additionally, this should work for you (to dissect): 此外,这应该适合你(解剖):

Param([string]$ad)

Write-Host $args[0]
Write-Host $ad

Read-Host
pause

Call the script with the path, powershell c:\\Path\\Folder\\Script.ps1 "Hello World!" $ad = "JSmith" 使用路径调用脚本,powershell c:\\Path\\Folder\\Script.ps1 "Hello World!" $ad = "JSmith" c:\\Path\\Folder\\Script.ps1 "Hello World!" $ad = "JSmith"

If this does not work, you should ensure that your execution policy is set correctly. 如果这不起作用,则应确保正确设置执行策略。 Get-ExecutionPolicy will tell you the current level. Get-ExecutionPolicy将告诉您当前的级别。 For testing you can set it very low with Set-ExecutionPolicy unrestricted 对于测试,您可以将Set-ExecutionPolicy unrestrictedSet-ExecutionPolicy unrestricted其设置得非常低

Add the following to the top of your script. 将以下内容添加到脚本的顶部。

Param(
  [Parameter(Mandatory=$true)]
  [string]$Euser
)

Write-Host "Deactivating $EUser"

Calling example after cd to the script directory cd之后调用示例到脚本目录

.\ScriptName.ps1 -Euser "FOO" # Tab auto completion works

The following in a new script works for me. 新脚本中的以下内容适合我。

Param([string]$servers)
"You chose $servers"

PS C:\\scripts> .\\Untitled1.ps1 "this test" PS C:\\ scripts>。\\ Untitled1.ps1“this test”

You chose this test 你选择了这个测试

PS C:\\scripts> PS C:\\ scripts>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM