我像下面那样编写了脚本,但是AD PowerShell模块是必需的,因此我做了一个检查命令并安装AD功能的函数。 但是我需要在执行参数之前执行此操作,如果我将函数放在“ param”之前,则会出现错误。有人知道如何在参数处理之前执行命令/功能吗?
脚本代码:
http://my.tms.server.com/layer/{z}/{x}/{yr}.png
功能代码:
Param(
$user="user",
$hosts = (Get-ADComputer -SearchBase 'ou=temp,dc=lab,dc=test,dc=com').Name,
$credential = (Get-Credential -Message "Type user/pass ")
)
Copy-Item -Path "path" -Destination "dest"
错误示例:
$user="user", + ~~~~~~~~~~~~~~~~~~~~~~~~~~ The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
答案 0 :(得分:1)
powershell脚本始终从参数处理开始。没有办法避免这种情况。但是我看不到需要将$ hosts添加到脚本的参数中。可以这样完成:
param(
$user="user",
[PSCredential]$credential=(get-credential -message "Type user/pass ")
)
Get-mandatory-modules
$hosts = (Get-ADComputer -SearchBase 'ou=temp,dc=lab,dc=test,dc=com').Name
Copy-item -path "path" -destination "dest"
但是,如果您希望能够将组织单位作为参数传递,则可以将其作为在脚本中使用的字符串来获取计算机名称:
param(
$user="user",
$hostsou = 'ou=temp,dc=lab,dc=test,dc=com'
[PSCredential]$credential=(get-credential -message "Type user/pass ")
)
Get-mandatory-modules
$hosts = (Get-ADComputer -SearchBase $hostsou).Name
Copy-item -path "path" -destination "dest"