by TechiBee
on August 10, 2011
PowerShell has a native module called “ActiveDirectory” using which we can query active directory. The advantage of using this is, you no need to depend on external installers like Quest PowerShell tools to query active directory. The only pre-requisites for using the cmdlets in ActiveDirectory module is you should run these from either vista or windows 7 computer and you environment should have atleast one Windows 2008 R2 DC(or should have ADWS installed on windows 2003 DCs).
So, here is the code to query active directory group for its members.
Import-Module ActiveDirectory
Get-ADGroupMember -Identity Group1 | select Name, ObjectClass
In the above code I am first importing ActiveDirectory module and the using Get-ADGroupMember cmdlet to query the group named Group1
If the Group1 has some more groups inside it, you can query the group recursively using the -recurse parameter.
ActiveDirectory some other useful cmdlets which you can use to manage group membership of Groups. For example, Add-ADGroupMember cmdlet helps us in adding a object to a group in AD. Similarly using Remove-ADGroupMeber we can remove objects from a group.
{ }
by TechiBee
on August 9, 2011
After you install Windows 2008 R2 Core Server from media, you notice that powershell is installed by default. PowerShell is essential in Core environment to administer core operating system. So, it is recommended to install powershell after you install Core operating system to take full advantage of this powerful programming language in configuring the several OS parameters. Today, I installed a core server and thought of writing a articles on how to install powershell in Core.
Procedure to install powershell in Windows 2008 R2 Server Core:
- Logon to the Server with administrator account and start server configuration wizard by typing “SCONFIG” in command prompt.
- Select option “4” in Server Configuration wizard to enter into “Configure Remote Management”
- Select option “2” to install powershell (Enable Windows PowerShell)
- Reboot the server if prompted
This completes the installation. Now you can start powershell by starting the process “powershell.exe” either from command prompt or from task manager.
Hope this helps…
{ }
by TechiBee
on August 9, 2011
In this post I am going to detail about querying various file attributes using powershell and some more operations related to that. Get-Item cmdlet helps you to fetch this information. Along with creationtime, it provides some details about the file you are querying. Let us go and see what they are…
Get file creation time:
(Get-item C:localadmincred.xml).creationtime
Get file modification time:
(Get-item C:localadmincred.xml).lastwritetime
Get file size:
"{0:N2}" -f ((Get-item C:localadmincred.xml).length/1mb) + " MB"
Get file extension:
Get-item C:localadmincred.xml | select extension
Get the directory name where file file is located:
dmincred.xml | select directory
Get file Security permissions:
(Get-item C:localdemo.xml).getaccesscontrol.invoke() | select owner -ExpandProperty access
Hope this helps…
{ }
by TechiBee
on August 9, 2011
This little powershell function allows you to get the size of given folder. It has two parameters, -Name which takes file path as argument and -recurse which gets you size of the folders including the recursive items. For this powershell function -name is mandatory parameter and can take values from pipeline by property name.
function Get-FolderSize {
[CmdletBinding()]
param(
[parameter(valuefrompipelinebypropertyname=$true,mandatory=$true)]
$Name,
[switch]
$recurse
)
begin {}
process {
if($recurse) {
$colItems = (Get-ChildItem $name -recurse | Measure-Object -property length -sum)
"Size of $name is -- " + "{0:N2}" -f ($colItems.sum / 1GB) + " GB"
}
else {
$colItems = (Get-ChildItem $name | Measure-Object -property length -sum)
"Size of $name is -- " + "{0:N2}" -f ($colItems.sum / 1GB) + " GB"
}
}
end {}
}
Output:
{ }
by TechiBee
on August 7, 2011
$file1version = [version](Get-Command .File1.dll).FileVersionInfo.FileVersion
$file2version = [version](Get-Command .File2.dll).FileVersionInfo.FileVersion
if($file1version -eq $file2version) {
write-host "File version numbers are equal"
} else {
write-host "File version numbers are not equal"
}
Above simple code compares file versions of two files. The comparison here is very straight forward. In the above code it first queries the file versions of two files, File1 and File2 and compares both of them to see if they are equal or not.
function Get-FileVersionInfo
{
param(
[Parameter(Mandatory=$true)]
[string]$FileName)
if(!(test-path $filename)) {
write-host "File not found"
return $null
}
return [System.Diagnostics.FileVersionInfo]::GetVersionInfo($FileName)
}
Above code is little bit enhanced which checks for file existence and returns the version numbers in object format which can be further used for other version control related operations.
{ }
by TechiBee
on July 13, 2011
Assigning a static IP address is one common task that system administrators do after building a server. In today’s post I am providing a powershell function(Set-StaticIP) which takes IP address, subnet mask, default gateway and DNS servers as parameters and assigns it to the computer where you are executing the function.
function Set-StaticIP {
<# .Synopsis Assings a static IP address to local computer. .Description Assings a static IP address to local computer. .Parameter IPAddress IP Address that you want to assign to the local computer. .Parameter Subnetmask Subnet Mask that you want to assign to the computer. This is optional. If you don't specify anything, it defaults to "255.255.255.0". .Parameter DefaultGateway Default Gateway IP Address that you want to assign to the local computer. .Parameter DNSServers DNS servers list that you want to configure in network connection properties. .Example Set-StaticIP -IPAddress 10.10.10.123 -DefaultGateay 10.10.10.100 -DNSServers 10.10.10.120,10.10.10.121 .Notes NAME: Set-StaticIP AUTHOR: Sitaram Pamarthi Website: www.techibee.com #>
[CmdletBinding()]
param (
[alias('IP')]
[Parameter(mandatory=$true)]
[string]$IPAddress,
[alias('Sb')]
[Parameter(mandatory=$false)]
[string]$Subnetmask = "255.255.255.0",
[alias('Gateway')]
[Parameter(mandatory=$true)]
[string]$DefaultGateway,
[alias('DNS')]
[parameter(mandatory=$false)]
[array]$DNSservers
)
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPenabled=TRUE" | % {
Write-host "Trying to set IP Address on $($_.Description)"
$_.EnableStatic($IPAddress,$Subnetmask) | out-null
$_.SetGateways($DefaultGateway) | out-null
$_.SetDNSServerSearchOrder($DNSServers) | out-null
}
if($?) {
write-host "IP Address set successfully"
} else {
write-host "Static IP assignment failed"
}
}
Above simple function assigns the IP address and displays the output of execution.
Feel free to post here if you have any questions about the script.
Happy learning.
{ }
by TechiBee
on July 12, 2011
System Center Virtual Machine Manager(SCVMM) is helps in managing physical and virtual IT infrastructure from central place. You can read about SCVMM at http://www.microsoft.com/systemcenter/en/us/virtual-machine-manager.aspx
SCVMM has powershell interface also which enables IT administrators to automate tasks. There are variety list of cmdlets related to SCVMM are available. You can download reference guide from here.
{ }
by TechiBee
on July 11, 2011
I inspired from my previous post, and decide to do some network interface related operations from command line as they helps me when managing Windows 2008 Core Operating system. Another command that I am going to provide now is to disable network connection from command line.
netsh interface set interface name=”Local Area Connection 1″ admin=DISABLED
In about command, “Local Area Connection 1” is the name of the connection that you want to disable. You can change the value of “Admin” to “Enable” to enable back the network connection. Similarly, if you want to rename the network connection, you can use newname parameter. Below is the command.
netsh interface set interface name=”Local Area Connection 1″ newname=”My NIC1″
Above command renames “Local Area Connection 1” network to “My NIC1”.
Hope this helps…
{ }
by TechiBee
on July 11, 2011
When you are working on Windows 2008 core, you get requirement to disable firewall functionality to allow all programs communication. The default firewall settings blocks most of the ports(including remote management) and administrator has to explicitly open them on need basis. The default Core console allows you to perform only few network operations like listing and assigning IP address, but if you want to perform advanced operations like disabling firewall functionality in all profiles(see my post https://techibee.com/windows-2008/what-is-domain-public-and-private-profiles-in-windows-2008-firewall/478 for different profiles in 2008 firewall), you need to rely on some command line options.
Netsh is one of the very useful command line utilities in windows environment. Now in this post, I will show you how to disable firewall profiles(Private, public, domain) on a windows 2008 Core. You need this command in Server Core environment because, you cannot manage firewall remotely as there is a provision and you can not manage it from local host as core supports no GUI. Only option is command line utility.
Now, let us see how we can disable firewall functionality from all profiles
netsh advfirewall set allprofiles state off
If you want to turn off the firewall for individual profiles use the below commands
netsh advfirewall set domainprofile state off
netsh advfirewall set privateprofile state off
netsh advfirewall set publicprofile state off
You can turn on firewall back by simply replacing “off” with “on” in above commands.
Netsh utility allows us to perform more set of operations on different components of operating system. You can explore the help(netsh /?) to lean about it further.
You can also download a NetSH Technical reference guide from MS website. Refer to https://techibee.com/general/download-netsh-technical-reference-for-windows-2008windows-7/494 for more details.
Hope this helps.
{ }
by TechiBee
on June 30, 2011
Yesterday I wrote about different ways to check how much charge is left in the laptop battery. After that I explored win32_battery WMI class further and got some more useful information.
How do you know your battery current status, like is it charging properly?, is it running low, it is full with charge, it is partially charged, is the charge is too low?. It is easy to answer this kind of questions if you can interpret batterystatus attribute of Win32_Battery WMI class. Let us see the possible values this attribute can hold and their meaning.
Value |
Meaning |
- 1
|
The battery is discharging. |
- 2
|
The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging. |
- 3
|
Fully Charged |
- 4
|
Low |
- 5
|
Critical |
- 6
|
Charging |
- 7
|
Charging and High |
- 8
|
Charging and Low |
- 9
|
Charging and Critical |
- 10
|
Undefined |
- 11
|
Partially Charged |
*Above table is copied from MSDN site.
Now let us write a script to check the battery current status.
Function Check-BatteryState {
param($Laptop=$env:computername)
$Bstatus = (Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus
if($Bstatus) {
switch ($Bstatus)
{
1 { "Battery is discharging" }
2 { "The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging." }
3 { "Fully Charged" }
4 { "Low" }
5 { "Critical" }
6 { "Charging" }
7 { "Charging and High" }
8 { "Charging and Low" }
9 { "Charging and Critical " }
10 { "Unknown State" }
11 { "Partially Charged" }
}
}
}
This powershell function helps you to identify current status of laptop battery. Hope this helps..
{ }