标签: powershell

设置 $PSDefaultParametersValues 以在模块范围内使用

我想我只是遗漏了一些东西,但这是我的设置,我有一个包含三个测试功能的测试模块,我想设置 $PSDefaultParameterValues 以创建一个仅存在于所述模块中的其他 cmdlet 的通用参数值。这是我的代码:

测试.psm1:

$PSDefaultParameterValues.Clear()

"$(Split-Path -Path $MyInvocation.MyCommand.Path)\*.ps1" | Resolve-Path | % { . $_.ProviderPath }
Run Code Online (Sandbox Code Playgroud)

在同一个目录中,我有我的功能:

新的TestFunction.ps1:

Function New-TestFunction
{
     [CmdletBinding()]
     [OutputType([String])]

     Param
     (
         [String]
         $InputString
     )

     $Script:PSDefaultParameterValues.Add("Write-TestValue:OutputValue", $InputString)
}
Run Code Online (Sandbox Code Playgroud)

写测试值.ps1:

Function Write-TestValue
{
     [CmdletBinding()]
     [OutputType()]

     Param
     (
          [Parameter()]
          [String]
          $OutputValue
     )

     Write-Output -InputObject $OutputValue
}
Run Code Online (Sandbox Code Playgroud)

所以,我应该能够做到这一点:

PS C:\> New-TestFunction -InputString 'Test String'; Write-TestValue
PS C:\> 'Test String'
Run Code Online (Sandbox Code Playgroud)

但它什么都不返回,$OutputValue 仍然是 $null。我让它工作的唯一方法是使用 $Global: 范围,我宁愿不这样做。我觉得在 Module 范围下导入的函数应该看到 $Script: Scope of variables。我写了一个小测试函数:

写脚本PSDefaultParameterValues.ps1

Function Write-ScriptPSDefaultParameterValues
{
    [CmdletBinding()]
    [OutputType()]

    Param
    ( …
Run Code Online (Sandbox Code Playgroud)

powershell scope module default-value

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

Exchange 远程 Powershell 出现零星的“损坏”状态

我正在尝试通过 Windows Powershell 从远程 Exchange 服务器接收所有 TransportAgent 的状态。

我偶尔会收到一个错误,即The session state is Broken. 一旦它坏了,我需要创建一个新的会话

下面是我正在使用的命令列表(按正确顺序)

# Build the PSSession for Exchange
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<SERVER>/PowerShell/ -Authentication Default
# Invoke the command 'Get-TransportAgent' on the remote PSSession
Invoke-Command $Session {Get-TransportAgent}
# Result when there is NO ERROR
Identity                                           Enabled         Priority        PSComputerName
--------                                           -------         --------        --------------
Transport Rule Agent                               True            1               <SERVER>
Malware Agent                                      True            2               <SERVER>
Text Messaging Routing Agent                       True            3               <SERVER>
Text Messaging Delivery …
Run Code Online (Sandbox Code Playgroud)

powershell exchange-server powershell-remoting

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

PowerShell Where-Object $_.name -like -in $list

我是 PowerShell 的新手,遇到了一些障碍。我正在尝试从多个服务器中提取程序名称和版本信息。

我在 $list 变量中有一个程序名称列表,但程序名称中还包含版本号。我只是在没有版本号的列表变量中存储程序的名称。

我试图找出一种方法,将 -like 和 -in 参数与Where-Object cmdlet 一起使用,以便将完整的程序条目名称(例如 AdToUserCacheSync 1.10.1.10)与我在 $list 变量中的条目(例如AdToUserCacheSync)。

我怎样才能做到这一点?

$list = Get-Content "\\server\c$\temp\list.txt"
$storeTestServers = Get-Content "\\server\c$\temp\testStores.txt"

foreach ($server in $storeTestServers) {

    Get-WmiObject -Class Win32_Product -ComputerName $server |
    Select-Object -Property PSComputerName, Name, Version |
    Where-Object {$_.pscomputername -like "940*" -and $_.name -like -in "*$list*"}
}
Run Code Online (Sandbox Code Playgroud)

powershell

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

Get-ADUser 错误:枚举上下文无效

前几天我发布了这个问题 从分组对象中提取电子邮件

$OuUser = @{}

$OuUser = Get-AdUser -Properties * -Filter * -SearchBase "domain"   

$Duplicates = $OuUser | Select samaccountname, mail,UserPrincipalName |
    Group-Object Mail | Where{$_.Count -gt 1}

$Duplicates | Select Name,Count,@{l='Accounts';e={($_.Group|Select -Expand samaccountname) -join ';'}} | 
    Export-CSV E:\Damo\Duplicates.csv -NoTypeInfo
Run Code Online (Sandbox Code Playgroud)

该代码在一个域上工作正常,并针对 OU 中的一小组用户对其进行测试。

在我想测试的域上进行测试时,其中有很多用户,此代码失败。OU 中包含非电子邮件格式的电子邮件地址。它指向 Get-ADUser 的错误。

Get-ADUser : The server has returned the following error: invalid enumeration c
ontext.
At C:\scripts\CountEmailsDup.ps1:4 char:21
+ $OuUser = Get-AdUser <<<<  -Properties * -Filter * -SearchBase 'ou=external,o
u=user accounts,dc=bizdir,dc=nzpost,dc=co,dc=nz' -SearchScope OneLevel
    + CategoryInfo …
Run Code Online (Sandbox Code Playgroud)

powershell active-directory

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

是否可以使用指定的(非默认)配置文件启动 Powershell ISE?

我想要多个(不止一个)PowerShell 配置文件来创建不同的环境。

更具体地说,我需要启动单独的 PowerShell ISE 以与 TFS 和其他 PowerShell ISE 实例一起工作以进行常规工作。“TFS”环境需要加载一些额外的管理单元、模块、修改提示等。我不希望在常规 PowerShell ISE 会话中执行所有这些内容,但仅在我想要的时候执行。

发现可以通过命令行参数-File自动加载任意脚本,但是不会自动执行..

powershell powershell-ise

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

删除父路径

以下脚本列出目录中的所有文件,并在控制台中返回它们的名称和路径。

结果是这样的:

C:\Projects\company\trunk\www\client\project\customer\start.js
Run Code Online (Sandbox Code Playgroud)

我需要删除初始部分并得到类似的结果

project\customer\start.js
Run Code Online (Sandbox Code Playgroud)

我无法在 Replace 中设置正确的正则表达式。你能指出我正确的方向吗?

C:\Projects\company\trunk\www\client\project\customer\start.js
Run Code Online (Sandbox Code Playgroud)

powershell

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

在 powershell 退出时运行脚本

我将如何运行可在exit命令或 shell 关闭上运行的 powershell 脚本。我希望这个脚本在每个关闭的 shell 上运行,而不仅仅是一个 shell。

脚本

windows powershell

3
推荐指数
2
解决办法
4811
查看次数

在 PowerShell 中创建时将 proxyAddresses 添加到 Active Directory 用户

我正在尝试在 Active Directory 中添加用户。这些用户需要有代理地址。我的问题是那些 proxyAddresses 是倍数并存储在一个数组中。

我尝试:

$proxyAddresses = @("address1@test.com", "address2@test.com", "address3@test.com")
$userInstance = new-object Microsoft.ActiveDirectory.Management.ADUser
$userInstance.ProxyAddresses = $proxyAddresses
New-ADUser test -Instance $userInstance
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

 Invalid type 'System.Management.Automation.PSObject'. Parameter name: proxyAddresses
Run Code Online (Sandbox Code Playgroud)

我想将此 proxyAddresses 数组添加到我的 AD 用户的属性 proxyAddresses 中,但似乎不可能。

知道如何做到这一点吗?

powershell active-directory

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

ArgumentList 为空时的 PowerShell Start-Process cmdlet

我已经尽可能地简化了我的情况。我有一个 PowerShell 脚本,它使用Start-Process cmdlet通过“以管理员身份运行”选项以编程方式启动进程:

param
(
    # Arguments to pass to the process, if any
    [string[]] $ArgList
)

if($ArgList)
{
    Start-Process -FilePath notepad.exe -ArgumentList $ArgList -Verb RunAs
}
else
{
    Start-Process -FilePath notepad.exe -Verb RunAs
}
Run Code Online (Sandbox Code Playgroud)

由于ArgumentList不能为 NULL 或为空,我不得不添加if-else以避免ParameterArgumentValidationError错误。

一切都按预期工作没有问题,但我只是想知道是否有更优雅(但仍然简单)的方法来完成它而不使用if-else块。

powershell arguments process

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

WMI/WQL 查询 powershell 中的双反斜杠

我的 WQL 查询遇到了奇怪的问题。

$SCCMQuery = @'
Select UAR.User, UAR.Application, UAR.CurrentState from sms_fullcollectionmembership as FCM
INNER JOIN SMS_UserApplicationRequest as UAR ON UAR.User=FCM.SMSID
where FCM.CollectionID="a\100104"
'@
$Results = Get-WmiObject -Namespace "root\SMS\Site_Name" -Query $SCCMQuery
Run Code Online (Sandbox Code Playgroud)

上面的查询无法正常工作,但是当我FCM.CollectionID像这样(a\\100104)添加另一个反斜杠时,它开始工作。

有人可以告诉我为什么它会这样工作吗?我无法在所有值中手动添加反斜杠,因为它们稍后会从其他 WMI 查询中生成。

powershell wmi wql

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