利用powershell修改ip属性
useing pipe
#Get-WmiObject Win32_NetWorkadapterConfiguration | Get-Member -MemberType Method
#get methods of Win32_NetWorkadapterConfiguration class
Get-WMIObject Win32_NetworkAdapterConfiguration | where {$_.IPEnabled -eq "true"}| ForEach-Object {$_.EnableStatic("192.168.10.247","255.255.255.0");$_.SetGateways("192.168.10.1") }
using var
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration | where {$_.IPEnabled -eq "true"}
Foreach($NIC in $NICs) {$NIC.EnableStatic("192.168.10.247","255.255.255.0");$NIC.SetGateways("192.168.10.1") }
我看了一下,没什么复杂的,只要利用几个method就ok了.首先得找好网卡,毕竟adapter有很多.还有好多微软免费提供的,所以需要找到lan的网卡,可以使用property:ipenabled=$true来做区别,当然也可以用别的,筛选需要修改ip属性的网卡后,可以新建一个变量指向它,或者直接挂属性,都可以.说一下过程
1.动态变静态
EnableStatic
The EnableStatic WMI class method enables static TCP/IP addressing for the target network adapter. As a result, DHCP for this network adapter is disabled.
This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
Syntax
uint32 EnableStatic( [in] string IPAddress[], [in] string SubnetMask[] );
Parameters
-
IPAddress [in]
-
Lists all of the static IP addresses for the current network adapter. Example: 155.34.22.0.
SubnetMask [in]
-
Subnet masks that complement the values in the IPAddress parameter. Example: 255.255.0.0.
SetGateways
The SetGateways WMI class method specifies a list of gateways for routing packets to a subnet that is different from the subnet that the network adapter is connected to. This method only works when the Network Interface Card (NIC) is in the static IP mode.
This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
Syntax
uint32 SetGateways( [in] string DefaultIPGateway[], [in] uint16 GatewayCostMetric[] );
Parameters
-
DefaultIPGateway [in]
-
List of IP addresses to gateways where network packets are routed.
GatewayCostMetric [in]
-
Assigns a value that ranges from 1 to 9999, which is used to calculate the fastest and most reliable routes. The values of this parameter correspond with the values in the DefaultIPGateway parameter. The default value for a gateway is 1. This optional parameter is new for Windows 2000.
SetDNSServerSearchOrder
The SetDNSServerSearchOrder WMI class method uses an array of string elements to set the server search order. This is an instance-dependent method call that applies on a per-adapter basis. After static DNS servers are specified to start using Dynamic Host Configuration Protocol (DHCP) instead of static DNS servers, you can call the method without supplying "in" parameters. In Windows 2000, the setting applies to the targeted adapter. In Windows NT, the setting is global.
This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
Syntax
uint32 SetDNSServerSearchOrder( [in] string DNSServerSearchOrder[] );
Parameters
-
DNSServerSearchOrder [in]
-
List of server IP addresses to query for DNS servers.
Example: 130.215.24.1 or 157.54.164.1
在这需要注意一下,setdnsserversearchorder只能接收一个dns地址,如果需要设置两个怎么办?用变量,比如$dnsservers="130.215.24.1","157.54.164.1 " 然后用SetDNSServerSearchOrder($dnsservers)就ok了,我也很奇怪,直接代进去就报错,还没找到原因
2. 静态设置动态
EnableDHCP
以前在ipconfig里面的release跟renew命令呢?可用下面两个method
ReleaseDHCPLease(ReleaseDHCPLeaseAll)
RenewDHCPLease(RenewDHCPLeaseAll)
基本上常用的就这些了,现在在找设置alternate configuration的method,还没找到,今天眼疼....
-------------------------------------------------------------------------------------
这里有一篇文章写的还不错
http://www.powershellpro.com/powershell-tutorial-introduction/powershell-wmi-methods/
Part 1 introduced us to WMI. We learned how to ‘find’ classes and examine their properties and methods. Hope you downloaded and familiarized yourself with WMI CIM Studio, as it is a great tool to have. We also learned how to find WMI Classes when working in PowerShell. Tutorial 11 Part 1 was about gathering data by enumerating properties within a WMI Class. In Part 2 of the PowerShell Tutorial for WMI, we are going to be using Methods to make changes in our environment. The example scripts make changes to the environment, I highly recommend running any script in a test environment before applying them in your production environment.
Working with Methods
Example 1.
Here’s where the fun begins… The Big Boss has come to your office after you have successfully implemented DHCP on your network. You figure he’s there to give you a “big” pat on the back and an even “bigger” raise (wishful thinking). The reality is, the desktop group is complaining because they don’t want to re-configure each workstation from static IP to DHCP. It’s not that they have an underlying hatred for DHCP, they just don’t want to physically visit all 4000 workstations to make the change – I don’t blame them… You look the Boss right in the eye and give him your standard answer, “NO PROBLEM” – I’ll take care of it. After all you are becoming a PowerShell GURU and you are no stranger to managing complex environments.
1. First step, what WMI Class are we going to use to accomplish this task? Using WMI CIM Studio, I did a search for “Network.” I found the Class “Win32_NetworkAdapterConfiguration” which has a method called “EnableDHCP” – Looks like a winner to me. Another important thing to note, this Class also has a property called “IPEnabled” which provides a boolean data type (True or False). The importance of this property will be apparent as we build the script.
2. I’m going test the script on my local workstation, so I verify that my NIC is setup as static:
3. Here is the script code that will change the NIC settings to “Obtain an IP address automatically”
| Where {$_.IPEnabled -eq “TRUE”}
foreach($NIC in $NICs) {$NIC.EnableDHCP()}
So what is the script doing?
- The first line stores the results of our WMI query in a variable called $NICs.
- It also utilizes the Property “IPEnabled” to filter active IP configured Network Adapters. This enables us to skip any disconnected, virtual, and/or non-IP configured NICs; such as NetBUI, IPX/SPX, AppleTalk, etc…
- We then use a foreach loop to iterate each item in the collection and use the “EnableDCHP” method to configure the NIC as a DHCP client.
You should see the following result:
4. Let’s verify we’ve received and IP address from DHCP. If you don’t have a DHCP server assigning addresses on your network, just follow the steps as we go. My address has changed from 192.168.171.42 (my static IP range) to 192.168.171.78 which is in my DHCP address pool, cool success! Okay… so we’re half way there with our code.
5. The next step is to change the DNS setting to “Obtain DNS server address automatically.” We want to change this so that we can utilize the DNS Scope Options created on the DHCP server. This was not a simple feat in VBScript, as we could not pass a “NULL” or and Empty String as we did with EnableDHCP(). Using PowerShell we are able to pass a “NULL” to the SetDNSServerSearchOrder method which sets the DHCP option. We are only adding one line to our script code $NIC.SetDNSServerSearchOrder()
| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$NIC.EnableDHCP()
$NIC.SetDNSServerSearchOrder()
}
Running the code should yield the following results:
Do an “ipconfig /all” to verify the DNS server settings as well as other scope options are available. You should also see DHCP Lease Obtain and Expires infomation.
Example 2.
I’m not going to leave you hanging, let’s say we want to use methods of the “Win32_NetworkAdapterConfiguration” Class to change to static IP or you wish to make changes in a static IP environment.
Let’s examine all the methods available to us:
| Get-Member -MemberType Methods | Format-List
Here is a list of the Methods that are available to this Class:
- DisableIPSec
- EnableDHCP
- EnableIPSec
- EnableStatic
- ReleaseDHCPLease
- RenewDHCPLease
- SetDNSDomain
- SetDNSServerSearchOrder
- SetDynamicDNSRegistration
- SetGateways
- SetIPConnectionMetric
- SetIPXFrameTypeNetworkPairs
- SetTcpipNetbios
- SetWINSServer
- ConvertFromDateTime
- ConvertToDateTime
- Delete
- GetType
- Put
Looking at the list, there are a number of methods we can use to remotely configure machines. Granted, the workstation is available on the network.
I’m going to share a script that is going to set the IP address, subnet mask, gateway, DNS, and WINS servers. But before I do a special note: Data Types, I bring it up again because knowing which “Data Type” is expected will help when using Methods. For example, let’s look at the Definitions for SetDynamicDNSRegistration and SetWINSServer Methods.
Name : SetDynamicDNSRegistration
Definition : System.Management…(System.Boolean FullDNSRegistrationEnabled…)
Right away you should notice that the Method uses a boolean argument (TRUE or FALSE). If you look at this setting graphically (Network Adapter properties – TCP/IP settings – Advanced Settings – DNS tab – Check box near the bottom) you will see that this setting is a check box. When checked the boolean value = TRUE, unchecked = FALSE.
In the real-world: A call comes in stating that a workstation was moved to another network subnet, when pinging its FQDN or doing a reverse DNS lookup it yields the old IP address. User can’t use application because network application requires name resolution (it can happen). You enumerate the “FullDNSRegistrationEnabled” property and it comes back with a setting of “FALSE.” Quickly you run the “FullDNSRegistrationEnabled(“TRUE”)” Method to resolve the issue. You’re a star!!!
Name :SetWINSServer
Definition: System.Management..(System.String WinsPrimaryServer, …)
Take note that this Method requires a text string data type. By now I’m sure we all know what a text string is.
Here is the script code:
| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$NIC.EnableStatic(“192.168.171.42″, “255.255.255.0″)
$NIC.SetGateways(“192.168.171.1″)
$DNSServers = “198.102.234.125″,”198.102.234.126″
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
$NIC.SetWINSServer(“198.102.234.125″, “198.102.234.126″)
}
Results:
IP Address, Subnet Mask, Default Gateway, DNS Servers configured
Register this connection’s addresses in DNS enabled.
WINS Configured.
Let’s take a closer look at the code:
| where{$_.IPEnabled -eq “TRUE”}
The first line of code creates a variable called $NICs and holds all the properties and methods for the “Win32_NetworkAdapterConfiguration” Class “where” the Network Adapter is IP Enabled. The “where” filter allows us to choose only real network adapters apposed to virtual and/or non-IP based NICs.
}
We now use a “Foreach” loop to iterate the $NICs collection.
$NIC.SetGateways(“192.168.171.1″)
$DNSServers = “198.102.234.125″,”198.102.234.126″
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
$NIC.SetWINSServer(“198.102.234.125″, “198.102.234.126″)
Within the foreach loop, we take an instance and use methods to assign values. Looking above we used the Method EnableStatic() to assign an IP address and subnet mask. Note: the IP address are integers but they have been encased in quotes as the Method requires a text string value. Also, each entry is separated by a comma.
$NIC.SetDNSServerSearchOrder($DNSServers)
These two lines of code are interesting, as I ran into trouble with the “SetDNSServerSearchOrder” method. I attempted to use the following but it would only allow me to set the preferred DNS server – SetDNSServerSearchOrder(“198.102.234.125″, “198.102.234.126″)
I thought it would work the same as setting the WINS servers, but look at the differences in each method’s definition:
SetWINSServer
(System.String WINSPrimaryServer, System.String WINSSecondaryServer)
SetDNSServerSearchOrder
(System.String[] DNSServerSearchOrder)
Looking up the “SetDNSServerSearchOrder” method on the Microsoft Developer site it states:
The SetDNSServerSearchOrder WMI class method uses an array of string elements to set the server search order.
Okay… an array of string elements. We’ve worked with arrays, we know how to build them. Look at the code again:
$NIC.SetDNSServerSearchOrder($DNSServers)
The first line creates an array and assigns values to the variable $DNSServers.
The second line uses the SetDNSServer… method and uses the values in $DNSServer.
Fun stuff right?!?
This is a good example of understanding which DataTypes are expected. Since I was puzzled by the definition, I looked up the Method on Microsoft Developers site and found the answer – phew!!!
Example 3.
In this example I’m going to show you how to format your hard drive… just kidding! But, if that were a task for you to complete, you should feel comfortable at this point with locating the WMI Class required and the Methods to do the job.
We are going to have a little fun, in this example were are going to use the “Win32_OperatingSystem” class to re-boot computers.
- Verify which Methods are available:
Get-WmiObject Win32_OperatingSystem | Get-Member -MemberType Method | Format-List
- Available Methods:
- Reboot
- SetDateTime
- Shutdown
- Win32Shutdown
Not much to it, we’ll use the “Reboot” Method to re-boot the computer. Don’t do it now, I’m just showing the code at this time.
- Local Re-boot code:
$colItems = Get-WmiObject Win32_OperatingSystem
Foreach($Item in $colItems){
$Item.Reboot()
}If you are a local Admin and you get the following error: “Privilege not held” there is a bug in .NET 1.0 SP3.
- The evil remote reboot. I just have to state this disclaimer: “Use this script only to resolve issues, do not use it to flame your fellow coworkers.” With that said, here is code to connected to a remote PCServer and reboot it. Save and run the code as RemBoot.ps1. The Code Prompts for the computer name you wish to reboot:
$strComputer = Read-Host “Enter Computer Name”
$colItems = Get-WmiObject Win32_OperatingSystem -ComputerName “$strComputer”
Foreach($Item in $colItems) {
$Item.Reboot()
}
Okay… we could go on for days with examples on how to use Methods of a WMI Class. My goal of the WMI tutorials are to show you how to find Classes and discover which Properties and Methods are available. This is enough to get you up and working with WMI. There is also a really cool “FREE” tool for scripting with WMI (WMI Explorer) that comes from “ThePowerShellGuy” website. I’ve made the tool available in the new PowerShellPro Forums. Get It, you will love it!!!
In the Next tutorial we are going to talk about creating reports, and I get to rant about Microsoft (just a little). I’m also going to show you what I consider the proper PowerShell syntax for WMI…
引用:http://siage1979.spaces.live.com/blog/cns!4BAF75E03B4EA23D!404.entry
更多推荐
所有评论(0)