PowerShell in Azure Cloud Shell

if you are an Azure user see this post from the PowerShell team – https://blogs.msdn.microsoft.com/powershell/2017/05/23/coming-soon-powershell-in-azure-cloud-shell/

Posted in Powershell, Azure | Leave a comment

Diskpart and PowerShell – part4: Remove a partition

So far you’ve seen how to create and modify partitions and volumes. Its now time to look at how you remove a partition.

Mount the test VHD

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

You can’t remove a volume – you have to remove the partition. Identifying the CORRECT partition to remove is the challenge

PS> Get-Partition | select PartitionNumber, DriveLetter, Size, Type

PartitionNumber DriveLetter         Size Type
--------------- -----------         ---- ----
              1                134217728 Reserved
              2           F   8589934592 Basic
              1                367001600 IFS
              2           C 511269232640 IFS
              3                470810624 Unknown

Not every partition has a drive letter and partition numbers are repeated. The partition object holds the disk number

PS> Get-Partition | select DiskNumber, PartitionNumber, DriveLetter, Size, Type | Format-Table

DiskNumber PartitionNumber DriveLetter         Size Type
---------- --------------- -----------         ---- ----
         1               1                134217728 Reserved
         1               2           F   8589934592 Basic
         0               1                367001600 IFS
         0               2           C 511269232640 IFS
         0               3                470810624 Unknown

So the combination of disk number and partition number is unique and will identify any partition. Remove our 8GB partition

Remove-Partition -DiskNumber 1 -PartitionNumber 2 -Confirm:$false

and the 128MB partition

Remove-Partition -DiskNumber 1 -PartitionNumber 1 -Confirm:$false

Get-Partition will show that the drive F: has been removed

Looking the disk organisation

Get-Disk -Number 1 | Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, @{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} | Format-List

Size              : 20
AllocatedSize     : 0
LargestFreeExtent : 20

The whole of the disk is now available for re-use

Posted in Powershell, Storage | Leave a comment

Diskpart and PowerShell–part 4: Expand a volume

Let’s create a new disk and mount it

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 20GB
Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD
Initialize-Disk -Number 1

This time we’ll create a volume that only uses part of the disk

New-Partition -DiskNumber 1 -DriveLetter F -Size 5GB

And now format the partition

Get-Partition -DriveLetter F |
Format-Volume -FileSystem NTFS -Confirm:$false –Force

The disk is organised like this

Get-Disk -Number 1 | 
Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, 
 @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, 
@{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} |
Format-List

Size              : 20
AllocatedSize     : 5.13
LargestFreeExtent : 14.87

Lets expand the partition

Get-Partition -DriveLetter F | 
Resize-Partition -Size 8GB

And re-examine the disk organisation

Get-Disk -Number 1 | 
Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, 
 @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, 
@{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} |
Format-List

Size              : 20
AllocatedSize     : 8.13
LargestFreeExtent : 11.87

The extra space is added to the volume and formatted to match the existing filesystem on the volume

Posted in Powershell, Storage | Leave a comment

Table or List

A question on the forum asked why a object is displayed in a table if it has 4 or fewer properties and as  a list if it  has more than 4 properties:

PS> [PSCustomObject]@{P1=1; P2=2; P3=3; P4=4}

P1 P2 P3 P4
-- -- -- --
 1  2  3  4
PS> [PSCustomObject]@{P1=1; P2=2; P3=3; P4=4; P5=5}


P1 : 1
P2 : 2
P3 : 3
P4 : 4
P5 : 5

This is a built in mechanism in PowerShell and is done automatically.

The quick way to get the output in table format is to use Format-Table

PS> [PSCustomObject]@{P1=1; P2=2; P3=3; P4=4; P5=5} | Format-Table

P1 P2 P3 P4 P5
-- -- -- -- --
 1  2  3  4  5
Posted in Powershell | Leave a comment

PowerShell v6 beta

PowerShell v6 has reached a significant milestone – the release of the first PowerShell v6 beta version. There have been 18 releases of alpha code since August 2016 when the open source PowerShell v6 project started.

There is no indication of how many beta releases there will be before PowerShell v6 is ready to ship.

Code is available from https://github.com/PowerShell/PowerShell/releases

Posted in PowerShell v6 | Leave a comment

Are your domain controllers real?

A question on the forum asked about discovering if domain controllers are physical or virtual machines.

This will do the job

foreach ($domain in (Get-ADForest).domains) { 
  Get-ADDomainController -filter * -server $domain | 
  sort hostname  |
  foreach { 
    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $psitem.Hostname |
    select PSComputerName, Manufacturer, Model
  }
}

Get the domains in your forest and then for each domain get the domain controllers. Get-ADDomainController outputs an object with a property of hostname – but you need a computername for Get-CimInstance. So, use a foreach-object and use the Hostname property as shown (you could create a property ComputerName on the pipeline object but its more work) and get the results. A virtual machine will show under the Model. You can sort or whatever once you have the results.

Posted in PowerShell and Active Directory, PowerShell and CIM | Leave a comment

OpenSSH

PowerShell v6 enables remoting over SSH between Linux and Windows machines. You can use remoting over SSH in any of these scenarios:

Windows – Linux
Linux – Windows
Windows – Windows
Linux – Linux

You can also establish traditional WinRM remoting sessions and send commands to a mixture of WinRM and SSH based sessions.

SSH connectivity is established using OpenSSH

A couple of new initiatives around OpenSSH need widespread participation.

First off – OpenSSH security testing

The PowerShell Team is getting OpenSSH production ready and as part of that are arranging for security testing. Details of how you can be involved are available from:

https://blogs.msdn.microsoft.com/powershell/2017/05/01/openssh-security-testing-kick-off/

Secondly – installing OpenSSH involves a significant number of manual steps. An OpenSSH Universal Installer is available for testing

https://cloudywindows.com/post/unveiling-the-openssh-universal-automated-installer/

Posted in PowerShell v6 | Leave a comment