Powershell

Powershell – Get Public IP

Today I write a quick tips 🙂 Sometime, we need to know the external public IP address for a computer using Powershell. This can be done easily with a Web Service of ipify.org.

Powershell Public IP

Powershell Public IP

(Invoke-WebRequest -uri "https://api.ipify.org/").Content

More

You can get more information about ipify.org here

Share
SCCM

Powershell – Execute SCCM 2012 Applications

Sometimes, it can be useful to execute an Application / Package in the SCCM Software Center using Powershell.
For example, if an application is only available, not required, in the deployment and you want to install several package without logging to the computer.

Unfortunately, there is no built-in powershell cmdlet to do this. We must call method with a specific dll.

Continue reading

Share
WindowsServer

Powershell – Add local Administrator

If you want to add Active Directory user or group to the local administrator group on a computer, you can use Powershell.

User and Group

To add user or group, we can use the cmdlet Invoke-Command associated with net localgroup.

Invoke-Command -ScriptBlock {net Localgroup administrators /add $args[0] } -ArgumentList("LAB\alexandre")

Computer

It is also possible to add a computer account into local Administrator group, you can use the command above but don’t forget to add the $ at the end of computer name.

Invoke-Command -ScriptBlock {net Localgroup administrators /add $args[0] } -ArgumentList("LAB\SRVSCCM$")

More

You can get more informations about net localgroup here.

Share
WindowsServer

Powershell – Change computer description

To continue in Powershell posts, we will see how to change the local description of the server. Not in Active Directory attribute but on the computer itself.

System Description

The local description is set is the WMI of the server. In the class Win32_OperatingSystem. To change it, we need to get the objects, set the new content and save the modification.

$OSWMI=Get-WmiObject -class Win32_OperatingSystem
$OSWMI.Description="My Server"
$OSWMI.put()

Remote

It is possible to modify the description on a remote computer, however, we need to adapt the script if the string is store in a variable.

With the Invoke-Command we add the parameter –ArgumentList, so that our variable content will be available on the execution on the remote host.

$myDescription="My Server"
Invoke-Command -ComputerName $lServerName -ScriptBlock {$OSWMI=Get-WmiObject -class Win32_OperatingSystem;$OSWMI.Description=$args[0];$OSWMI.put() } -ArgumentList($myDescription)

More

You can get more information about Win32_OperatingSystem class here

Share
WindowsServer

Powershell – How to format all disks

Did you know that you can manage disks, partitions and volumes using Powershell? Microsoft released a lot of cmdlet in order to facilitate handling of disks.

With virtualization, you can easily add disk to your server, but you have to create the volume yourself. I wrote a script that you can integrate in your process, runbooks or others.

Disks initialization

First thing to do, is to prepare disks to host volume. We need to set disks online and to disable Read only.

try {
#Set all disks, except the first disk, to online and writable
Get-Disk | ?{$_.number -ne 0}| Set-Disk -IsOffline $False
Get-Disk | ?{$_.number -ne 0}| Set-Disk -isReadOnly $False
 
#Initialize all disks
Get-Disk | ?{$_.number -ne 0}| Initialize-Disk -PartitionStyle GPT
}catch{
Write-Host $_.Exception.Message
}

Volume

Once disks are ready, we need to create, and format volume.

try {
#Create Partition on all disk, auto assign letter and use maximum size
Get-Disk | ?{$_.number -ne 0}| New-Partition -AssignDriveLetter -UseMaximumSize
#Get all partitions and format them
Get-Disk | ?{$_.number -ne 0}| Get-Partition |?{$_.type -like "Basic"}| Format-Volume -Confirm:$false
}catch{
Write-Host $_.Exception.Message
}

Remote

If you want to execute these command on a remote host, it is possible. We will use the cmdlet Invoke-Command

try {
#Set all disks, except the first disk, to online and writable
Invoke-Command -ComputerName $lServerName -ScriptBlock {Get-Disk | ?{$_.number -ne 0}| Set-Disk -IsOffline $False}
Invoke-Command -ComputerName $lServerName -ScriptBlock {Get-Disk | ?{$_.number -ne 0}| Set-Disk -isReadOnly $False}
#Initialize all disks
Invoke-Command -ComputerName $lServerName -ScriptBlock {Get-Disk | ?{$_.number -ne 0}| Initialize-Disk -PartitionStyle GPT}
#Create Partition on all disk, auto assign letter and use maximum size
Invoke-Command -ComputerName $lServerName -ScriptBlock {Get-Disk | ?{$_.number -ne 0}| New-Partition -AssignDriveLetter -UseMaximumSize}
#Get all partitions and format them
Invoke-Command -ComputerName $lServerName -ScriptBlock {Get-Disk | ?{$_.number -ne 0}| Get-Partition |?{$_.type -like "Basic"}| Format-Volume -Confirm:$false }			
}catch{
Write-Host  $_.Exception.Message
}

More

You can get more information about Disk Management cmdlet here.

Share
Active Directory 2012

Active Directory – Create OU using Powershell

Sometimes, you need to create a lot of Organization Units into your Active Directory.
To do this, you can use the Active Directory Users and Computers but it can be quickly time consuming depends on the number of OU.

Hopefully, Microsoft integrated a powershell module with Active Directory. So, we can create a script to do this for us.

Continue reading

Share
WindowsServer

Powershell – Create Shared Folder for DFS

When you want to use DFS (Distributed File System), and get benefit of the replication, you have to create a shared folder and install features on each server. It can be time consuming to do this for a lot of server.

To create it quickly, you can create a package on SCCM with a Powershell script, and deploy it to all of your servers.

Create shared Folder for DFS

You can find below a powershell script to execute :

  1. Create a folder.
  2. Share it with a specific group.
  3. Install Windows Features to enable DFS Replication.
New-Item "D:\Share\MyFolder" -type directory
 
New-SMBShare –Name "MyShareName" –Path "D:\Share\MyFolder" -ReadAccess "everyone"
 
Install-WindowsFeature FS-DFS-Replication, RSAT-DFS-Mgmt-Con

You can find more information about DFS : http://technet.microsoft.com/en-us/library/jj127250.aspx

Share
WindowsServer

Powershell – How to disable NetBios

Disable Netbios

If you want to use Powershell to disable Netbios on several computers, you can execute this script:

$adapters=(gwmi win32_networkadapterconfiguration )
Foreach ($adapter in $adapters){
  Write-Host $adapter
  $adapter.settcpipnetbios(2)
}

Options

Parameters for settcpipnetbios are:

  • 0: Enable Netbios via DHCP.
  • 1: Enable Netbios on the interface.
  • 2: Disable Netbios on the interface.

For more information on this function: http://msdn.microsoft.com/en-us/library/aa393601(v=vs.85).aspx

Verification

After the script was runned, you will have this configuration:
Disable netbios

Share