无法将计算机移动到目标 OU - PowerShell 脚本 [英] Cannot Move Computer to Target OU - PowerShell Script

查看:62
本文介绍了无法将计算机移动到目标 OU - PowerShell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此 PowerShell 脚本的以下错误,该脚本连接到远程 DC 并导入 AD 模块以及获取本地计算机名称以根据名称将其移动到指定的 OU

I am getting the below error for this PowerShell script that connects to a remote DC and imports the AD module as well as gets the local computer name to move it to specified OU based on the name

$remServer="DC1"
$s=new-PSSession -Computer $remServer
Invoke-Command -Session $s -script {Import-Module ActiveDirectory}
Import-PSSession -Session $s -module ActiveDirectory 

$movePC = gc env:computername

if($movePC.substring(5,3) -imatch "Dbs"){
$TargetOU ='ou=DB Servers,ou=PRD,ou=Servers,dc=com,dc=company,dc=net'
Get-ADComputer $movePC | Move-ADObject  -Targetpath $TargetOU}

Remove-PSSession -Session $s


Script     1.0        tmp_54y3mzbf.25i                    {Add-
ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-
ADDomainControllerPasswordReplicationPolicy, Add-...
The input object cannot be bound to any parameters for the command either 
because the command does not take pipeline input or the input and its 
properties do not match any of 
the parameters that take pipeline input.
+ CategoryInfo          : InvalidArgument: (CN=123123DBSTST0...company,DC=net:PSObject) [Move-ADObject], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
+ PSComputerName        : DC1**

推荐答案

试试这个:

$Server = 'DC1'
$s = New-PSSession -Computer $Server
Invoke-Command -Session $s -ScriptBlock {Import-Module -Name 'ActiveDirectory'}
Import-PSSession -Session $s -Module ActiveDirectory 

If ($env:ComputerName.Substring(5,3) -eq 'dbs')
{
    $TargetOU = 'ou=DB Servers,ou=PRD,ou=Servers,dc=com,dc=company,dc=net'
    $TargetPC = Get-ADComputer -Identity $env:ComputerName
    If ($TargetPC)
    {
        Move-ADObject -Identity $TargetPC -TargetPath $TargetOU -TargetServer $Server
    }
    Else
    {
        Write-Host "Failed to find $env:ComputerName on domain!"
    }
}

Remove-PSSession -Session $s

这篇关于无法将计算机移动到目标 OU - PowerShell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆