【发布时间】:2017-10-10 17:53:24
【问题描述】:
我有一个脚本,当我在域控制器上本地运行时,它可以正常工作(在本地使用 Import-Module ActiveDirectory 而不是远程 PSSession)。但是,当我远程运行它时,它会失败并出现以下错误:
我运行的代码是:
$ADSession = New-PSsession -ComputerName ourdcserver -Credential $(Get-Credential)
Invoke-Command -Command {
Import-Module ActiveDirectory,AdSync -Cmdlet Get-ADUser, Search-ADAccount
} -Session $ADSession
Import-PSSession -Session $ADSession -AllowClobber -Module ActiveDirectory
Search-ADAccount -UsersOnly -AccountInactive -SearchBase "OU=test,DC=our,DC=domain,DC=com" -Timespan 30.00:00:00 |
Where-Object { $_.Enabled -eq $true } |
Get-ADUser -Properties Name, sAMAccountName, WhenCreated, lastLogonTimestamp |
Select Name, sAMAccountName, WhenCreated,
@{n="LastLogonTimeStamp";e={[DateTime]::FromFileTime($_.LastLogonTimestamp)}} |
Sort-Object LastLogonTimeStamp |
Export-CSV $CurrentWorkfile -NoTypeInformation -Encoding ASCII
我发现了一个类似的线程 on this Technet forums thread 但是我对 PowerShell 不是很熟悉。谁能帮我重写这段代码(或示例)以使管道正常工作?
【问题讨论】:
-
尝试使用反引号
`分隔所有新行条目,然后从下一行开始执行命令。我认为如果您将 Search-ADAccount 查询保留在编辑器中的一行中,它会起作用! -
此示例中的换行符很好。不需要反引号。
标签: windows powershell active-directory