很多的企业都有这样的要求:希望整理自己的computers容器中的计算机,根据计算机名将其添加到对应的OU中去,但如果由人力来实现的话,工作量非常大,所以,在这次给某个企业实施项目的时候,我随手写了一个脚本,该脚本会自动将计算机加入到对应的OU中去。用户可以自己改写一下脚本,以适应自己的情况,我的脚本里对计算机名截取了2个字符。
dim strADsPath
dim sResultText
Const ADS_SCOPE_SUBTREE = 2
Set con = CreateObject("ADODB.Connection")
Set com = CreateObject("ADODB.Command")
'Open the connection with the ADSI-OLEDB provider name
con.Provider = "ADsDSOObject"
con.Open
Com.ActiveConnection = con
Com.CommandText = "<LDAP://cn=computers,DC=contoso,DC=com>;" & "(objectClass=computer);name;subTree"
Set rs = Com.Execute()
i=0
Do Until rs.EOF
sResultText=rs.Fields("name").value
Select Case left(sResultText,2)
Case "c1"
strADsPath="LDAP://cn=" & sResultText & ",cn=computers,dc=contoso,dc=com"
set objou=getobject("LDAP://ou=ouc1,dc=contoso,dc=com")
objou.movehere strADsPath,vbNullString
Case "c2"
strADsPath="LDAP://cn=" & sResultText & ",cn=computers,dc=contoso,dc=com"
set objou=getobject("LDAP://ou=ouc2,dc=contoso,dc=com")
objou.movehere strADsPath,vbNullString
Case "c3"
strADsPath="LDAP://cn=" & sResultText & ",cn=computers,dc=contoso,dc=com"
set objou=getobject("LDAP://ou=ouc3,dc=contoso,dc=com")
objou.movehere strADsPath,vbNullString
Case "c4"
strADsPath="LDAP://cn=" & sResultText & ",cn=computers,dc=contoso,dc=com"
set objou=getobject("LDAP://ou=测试,dc=contoso,dc=com")
objou.movehere strADsPath,vbNullString
End Select
rs.MoveNext
Loop
con.close
然后,在控制面板的“定期任务”中,将此脚本指定多少时间执行一次,然后输入执行的用户和密码,即可。
收工,撤!
跟我从头学powershell(四)
操作符
一.数学操作符
数学操作符允许你进行数学运算:
|
Operator |
Description |
Example |
|
+ |
Add two values together. |
PS C:\> 5+4
9 |
|
- |
Subtract one value from another. |
PS C:\> 134-90
44 |
|
- |
Change a value to a negative number. |
PS C:\>-6
-6 |
|
* |
Multiply two values together. |
PS C:\> 3*4.5
13.5 |
|
/ |
Divide one value by another. |
PS C:\> 6/4
1.5 |
|
% |
取模. |
PS C:\Temp> 6%4
2 |
优先级:
就和小学学得一样,各操作符之间有优先级。括号压倒优先级
变量:
变量在使用数学操作符的时候,和数字没有什么两样,但下面一个例子,则报错,因为我们采用的一个变量是字符串,一个是数字:
PS C:\> $var1="Windows"
PS C:\> $var2=100
PS C:\> $var1+var2
You must provide a value expression on the right-hand side of the '+'
operator.
At line:1 char:7
+ $var1+v <<<< ar2
PS C:\>
一元操作符
一元操作符通常用于增加或减少一个固定值
PS C:\> $var=10
PS C:\> $var++
PS C:\> $var
11
PS C:\> $var--
PS C:\> $var
10
PS C:\>
二.逻辑操作符
Powershell的逻辑操作符用来测试和检查一个表达式,通常的结果是TRUE 或 FALSE。
|
Operator |
Description |
Example |
|
-and |
All expressions must evaluate as TRUE. |
(1 -eq 1) -and (2 - eq 2) returns TRUE |
|
-or |
At least one expression must evaluate as TRUE. |
(1 -eq 1) -or (2 -eq 4) returns TRUE |
|
-not |
Evaluates the inverse of one of the expressions. |
(1 -eq 1) -and -not (2 -gt 2) returns TRUE |
|
! |
The same as -not. |
(1 -eq 1) -and ! (2 -gt 2) returns TRUE |
逻辑操作符通常用于你需要比较多个条件的情况。下面是举例,第一个是and情况,第二个是or逻辑操作符:
PS C:\> $varA=5
PS C:\> $varB=5
PS C:\> if (($varA -eq $varB) -and ($varB -gt 20))
>>{
>>Write-Host "Both conditions are true."
>>}
>>else
>>{
>>Write-Host "One or both conditions are false."
>>}
>>
One or both conditions are false.
PS C:\>
用or逻辑操作符:
PS C:\> if (($varA -eq $varB) -or ($varB -gt 20))
>> {
>> Write-Host "At least one condition is true."
>> }
>> else
>> {
>> Write-Host "Both conditions are false."
>> }
At least one condition is true.
PS C:\>
三.分配操作符:
Powershell采用分配操作符设定变量值,我们在前面已经采用了等号,但还有一些其他的操作符:
|
Operator |
Description |
|
= |
Sets a value of a variable to the specified value. |
|
+= |
Increases the value of a variable by the specified value or appends to the existing value. |
|
-= |
Decreases the value of a variable by the specified value. |
|
*= |
Multiplies the value of a variable by the specified value or appends to the existing value. |
|
/= |
Divides the value of a variable by the specified value. |
|
%= |
Divides the value of a variable by the specified value and assigns the remainder (modulus) to the variable. |
下面是这些操作符的一些示例:
PS C:\> $var=7
PS C:\> $var
7
PS C:\> $var+=3
PS C:\> $var
10
PS C:\
PS C:\> $var-=3
PS C:\> $var
7
PS C:\>
PS C:\> $var*=3
PS C:\> $var
21
PS C:\>
PS C:\> $var/=7
PS C:\> $var
3
PS C:\>
PS C:\> $var=9
PS C:\> $var%=4
PS C:\> $var
1
PS C:\>
你必须留意赋值操作,因为powershell尽量采用“最好的”方式来理解你的意图,比如下面一个举例:
PS C:\> $var="3"
PS C:\> $var+=7
PS C:\> $var
37
PS C:\>
因为“3”是字符串,所以,powershell就用简单的连接来代替掉了相加,你如果对你的变量的类型不能确定的话,你可以使用gettype()方法:
PS C:\> $var.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\>
在采用“=”赋值的时候,powershell会有一些独特的特性,比如,你给变量赋予16进制的值时,会自动转变成10进制显示:
你甚至可以用一些标准的术语,比如KB,MB,GB等等:
PS C:\> $var=10KB
PS C:\> $var
10240
PS C:\> $var=2MB
PS C:\> $var
2097152
PS C:\> $var=.75GB
PS C:\> $var
805306368
PS C:\>
最后,我们可以在一行当中为多个变量赋值:
PS C:\> $varA,$varB,$varC="Apple",3.1416,"Windows"
PS C:\> get-variable var?
Name Value
---- -----
varC Windows
varB 3.1416
varA Apple
PS C:\>
当然如果你提供的值超过变量,那么将把多出来的值赋予给最后一个变量:
PS C:\> $varA,$varB,$varC="Apple",3.1416,"Windows","Linux"
PS C:\> get-variable var?
Name Value
---- -----
varC {Windows, Linux}
varB 3.1416
varA Apple
PS C:\>
四.位操作符
位操作符用于二进制计算:
|
Operator |
Definition |
|
-band |
binary and |
|
-bor |
binary or |
|
-bnot |
binary not |
下面是一个实例:
PS C:\> 255 -band 255
255
PS C:\> 255 -band 150
150
PS C:\> 32 -bor 16
48
PS C:\>
五.特殊操作符
Replace 操作符:
Replace操作符可以替换字符,这个操作本质上是找到目标字符,然后进行替换:
其语法是:
"String-to-search" -replace "Search-for","Replace-with"
下面是一个示例:
PS C:\> "PowerShell" -replace "e","3"
Pow3rSh3ll
PS C:\> "PowerShell" -replace "shell","tool"
Powertool
PS C:\> "PowerShell" -replace "k","m"
PowerShell
PS C:\>
你也可以采用-replace操作符来对变量操作,但是注意的是,它不改变变量的初始值,唯一改变的方法是重新为变量赋值。
PS C:\> $var=$var -replace "p","sh"
PS C:\> $var
showerShell
PS C:\>
我们也在数组中使用-replace
PS C:\> $var=@("aaa","bbb","abab","ccc")
PS C:\> $var
aaa
bbb
abab
ccc
PS C:\> $var=$var -replace "a","z"
PS C:\> $var
zzz
bbb
zbzb
ccc
PS C:\>
我们甚至可以在文本文件上采用-replace操作符:
PS C:\> $var=get-content "boot.ini"
PS C:\> $var -replace "windows","WIN"
[boot loader]
timeout=15
default=multi(0)disk(0)rdisk(0)partition(2)\WIN
[operating systems]
multi(0)disk(0)rdisk(0)partition(2)\WIN="Microsoft WIN XP
Professional" /fastdetect /NoExecute=OptIn
multi(0)disk(0)rdisk(0)partition(1)\WIN="WIN Server 2003, Enterprise"
/noexecute=optout /fastdetect
C:\CMDCONS\BOOTSECT.DAT="Microsoft WIN Recovery Console" /cmdcons
PS C:\>
注意,-replace是不区分大小写的。如果你要区分大小写,应该使用-creplace操作符。
下面是这些操作符的一个表格:
|
Operator |
Definition |
Example |
|
-replace |
Replace |
"PowerShell" - replace "s","$" |
|
-ireplace |
Case-insensitive replace. Essentially the same as -replace. |
"PowerShell" - replace "s","$" |
|
-creplace |
Case-sensitive replace. |
"PowerShell" - creplace "p","t" |
PS C:\> "PowerShell" -creplace "p","t"
PowerShell
PS C:\> "PowerShell" -creplace "P","t"
towerShell
PS C:\>
Type操作符
Powershell包括三种类型的type操作符
|
Operator |
Definition |
Example |
|
-is |
Check if object IS a specific type. |
$var -is [string] |
|
-isnot |
Check if object IS NOT a specific type. |
$var -isnot [string] |
|
-as |
Convert object to specified type. |
3.1416 -as string |
操作的结果对于-is和-isnot是true和false
PS C:\> $now=get-date
PS C:\> $now -is [datetime]
True
PS C:\> 1024 -is [int]
True
PS C:\> "Microsoft" -isnot [string]
False
PS C:\>
-as操作符将转换对象成为指定的类型:
PS C:\> $var=get-date
PS C:\> $var.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------