通过 PowerShell 自动将主机名移动到 OU

问题描述

我有一个脚本,用于通过 PowerShell 自动将主机名移动到 OU。但根据我的脚本,它不起作用。我已经尽我所能,但它没有发生.. 任何人都可以帮助我按照以下要求修改我的脚本。

    # will match a d,l,D or L character before the digit
[regex]$regex = "[dlDL](?=\d)"


Get-Content -Path C:\Users\ssgrp0312\Music\uptime\move.txt | ForEach-Object {
      # check if the hostname matches the regex
    $match = $regex.Match($_)
# If MASU
    if { (  $_.Substring(0,4) = masu )
     If($match.Success){
                 if  (  $_.Substring(4,3) = thn )
             If($match.Success){
                # if client is L -> move to laptops
                    If($match.Value.toupper() -eq "L"){
                         Write-Host "$_ is a laptop" -ForegroundColor Green
                            Get-ADComputer $_ | Move-Adobject -TargetPath "OU=Laptops,OU=G-Corp,OU=MASU,DC=ABC,DC=COM"
                     # if client is D -> move to desktops
                     }Elseif($match.Value.toupper() -eq "D"){
                        Write-Host "$_ is a desktop" -ForegroundColor Green
                        Get-ADComputer $_ | Move-Adobject -TargetPath "OU=desktops,DC=COM"
                    }Else{
                        Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
                     }

        Elseif  (  $_.Substring(4,2) = ib )
             If($match.Success){
                # if client is L -> move to laptops
                    If($match.Value.toupper() -eq "L"){
                         Write-Host "$_ is a laptop" -ForegroundColor Green
                            Get-ADComputer $_ | Move-Adobject -TargetPath "OU=Laptops,OU=Indiabulls,2) = rt )
             If($match.Success){
                # if client is L -> move to laptops
                    If($match.Value.toupper() -eq "L"){
                         Write-Host "$_ is a laptop" -ForegroundColor Green
                            Get-ADComputer $_ | Move-Adobject -TargetPath "OU=Laptops,OU=rtech,DC=COM"
                    }Else{
                        Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
                     }



        Else{  
                # if client is L -> move to laptops
                    If($match.Value.toupper() -eq "L"){
                         Write-Host "$_ is a laptop" -ForegroundColor Green
                            Get-ADComputer $_ | Move-Adobject -TargetPath "OU=Laptops,OU=branches,DC=COM"
                    }Else{
                        Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
                     }}
    }

# If MAZI
    if { (  $_.Substring(0,4) = mazi )
     If($match.Success){
                 if  (  $_.Substring(4,OU=MAZI,DC=COM"
                    }Else{
                        Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
                     }}
    }

# If MANS
    if { (  $_.Substring(0,4) = mans )
     If($match.Success){
                 if  (  $_.Substring(4,OU=MANS,DC=COM"
                    }Else{
                        Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
                     }}
    }

Else {
 Write-Host "$_ not as per naming conversion" -ForegroundColor Red
}

示例:

MASUDFGD001 -- ABC\MASU\BRANCHES\DESKTOPS MASUDFGL001 -- ABC\MASU\BRANCHES\笔记本电脑 MASUTHNFD001 -- ABC\MASU\G-Corp\DESKTOPS MASUIBFGD001 -- ABC\MASU\Indiabul\DESKTOPS

MAZIGHJUD003 --  ABC\MAZI\BRANCHES\DESKTOPS
MAZIGHJUL003 --  ABC\MAZI\BRANCHES\LAPTOPS
MAZITHNFD001 --  ABC\MAZI\G-Corp\DESKTOPS
MAZIIBFGD001 --  ABC\MAZI\Indiabul\DESKTOPS

MANSGHJUD003 --  ABC\MANSYU\BRANCHES\DESKTOPS
MANSTHNFD001 --  ABC\MANSYU\G-Corp\DESKTOPS
MANSTHNFL001 --  ABC\MANSYU\G-Corp\LAPTOPS
MANSIBFGD001 --  ABC\MANSYU\Indiabul\DESKTOPS

MANSGHKSK003 --  ABC\MAMBU\KISOK
MANSTHKSK001 --  ABC\MAMBU\KISOK
MANSIBKSK001 --  ABC\MAMBU\KISOK

规则:

In Last before digits if L comes laptop
IF D comes Desktop 
IF KSK comes direct move to ABC\MAMBU\KISOK

In first MASU comes should move to ABC\MASU\*
If MAZI comes should move to ABC\MAZI\*
If MANS comes ABC\MANSYU\*
If MAHT Comes ABC\MAHTGB\*

After these letters check next 3 letters
If IB comes => Indiabul
If THN Comes => G-corp
Anything Else => Branches

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)