Created
September 14, 2017 05:44
-
-
Save keithga/cf33f4e89cb811faee4260842ad77f11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Generate a computer list from Hyper-V ready to import into Configuration Manager | |
.DESCRIPTION | |
Given a Hyper-V server and a set of Hyper-V Virtual Machines, this script will | |
extract out the necessary information required to create the associated Machine Object in | |
Configuration Manager. | |
.PARAMETER Path | |
Name of the CSV file to be created | |
.PARAMETER SourceComputer | |
Optional parameter used to pre-populate the SourceComputer Field in the CSV output. | |
.PARAMETER Role | |
Optional Paramater used to pre-populate the | |
.NOTES | |
If you modify this file in Excel, you should save the file in "CSV (MS-DOS) *.csv" format to ensure there are no extra double-quotes present. | |
.EXAMPLE | |
.\Get-VMListFOrCM.ps1 | ft | |
Get all virtual machines and display in a table. | |
.EXAMPLE | |
.\Get-VMListFOrCM.ps1 | convertto-csv -NoTypeInformation | % { $_.replace('"','') } | |
Find all Virtual Machines and convert to a CSV format without any doublequotes. | |
.EXAMPLE | |
.\Get-VMListFOrCM.ps1 -Verbose -name hyd-cli* -Role ROLE_Test1 -path .\test.csv | |
Find all Virtual Machines that start with the name HYD-CLI, and export to .\test.csv | |
.LINK | |
https://technet.microsoft.com/en-us/library/bb633291.aspx | |
#> | |
[cmdletbinding()] | |
param ( | |
[string[]] $Name, | |
[string] $computerName, | |
[pscredential] $Credential, | |
[string] $path, | |
[string] $SourceComputer = '', | |
[string] $Role = '' | |
) | |
$GetVMProp = @{} | |
if ( $computerName ) { $GetVMProp.add( 'ComputerName',$computerName ) } | |
if ( $Credential ) { $GetVMProp.add( 'Credential',$Credential ) } | |
$VitSetData = get-wmiobject -Namespace "Root\virtualization\v2" -class Msvm_VirtualSystemSettingData @GetVMProp | |
if ( $Name ) { $GetVMProp.add( 'Name',$Name ) } | |
write-verbose "Extract data from Hyper-V" | |
$Results = Get-VM @GetVMProp | | |
ForEach-Object { | |
[PSCustomObject] @{ | |
ComputerName = $_.Name | |
'SMBIOS GUID' = $VitSetData | | |
Where-Object ConfigurationID -eq $_.VMId.Guid | | |
ForEach-Object { $_.BIOSGUID.Trim('{}') } | |
'MAC Address' = $_ | Get-VMNetworkAdapter | select-object -first 1 | ForEach-Object { $_.MacAddress -replace '..(?!$)', '$&:' } | |
'Source Computer' = $SourceComputer | |
'Role001' = $Role | |
} | |
} | |
$Results | out-string -Width 200 | Write-Verbose | |
write-verbose "write out to CSV file" | |
if ( $path ) { | |
$Results | | |
ConvertTo-Csv -NoTypeInformation | | |
ForEach-Object { $_.replace('"','') } | | |
Out-File -FilePath $path -Encoding ascii | |
write-verbose @" | |
You can now import this list into CM: | |
############################################# | |
# Sample Script to import into Config Manager | |
import-module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' | |
get-psdrive | Where-Object { $_.Provider.name -eq 'CMSite' } | Select-object -first 1 | ForEach-Object { set-location `"`$(`$_.name)``:`" } | |
Import-CMComputerInformation -CollectionName "All Systems" -FileName "$Path" -VariableName Role001 | |
############################################# | |
"@ | |
} | |
else { | |
$Results | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment