How to send notification mail via powershell - powershell

I want to send notification mail after vm deploy like below.
My question is :
1 - If DOMAIN field 'Y' then TRUE else will WORKGROUP
2- IF BACKUP IP field 'N' then it will "not defined"
3- Lastly , I have $diskSizes variable. A VM have one or more disks. $diskSizes[0 -> Hard disk 2] , $diskSizes[1 -> Hard disk 3] and so on.
My script:
$VCServer = Read-Host "Enter the vCenter server name"
Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
New-Vm .... blah blah
$diskSizes = #()
do {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if($diskSize -ne 'no'){
$diskSizes += $diskSize
}
}
until($diskSize -eq 'no')
if($diskSizes.Count -gt 0){
$diskSizes | %{
New-HardDisk -VM $row.ServerName -CapacityGB $_ | Out-Null
}
}
....
...
$Report = [PSCustomObject]#{
'VMName' = $row.ServerName
'OS' = $row.ServerName
'DOMAIN' = $row.DOMAIN
'LAN IP' = $row.LANIP
'BACKUP IP' = $row.BACKUPIP
'VMState' = (Get-VM -Name $row.ServerName).summary.runtime.powerState
'TotalCPU' = $row.NumCPU
'Totalmemory' = $row.MemoryGB
'vCenter' = $VCServer
'VMHost' = $row.ESXHOST
}
Send-MailMessage .....
}
My CSV File:
ServerName ESXHOST Datastore OSCapacityGB NumCPU MemoryGB NetworkName Second Network Adapter LANIP LANGW BACKUPIP DOMAIN
TestVM01,192.168.30.10,LUNPRDVM01,50,4,16,PG_VLAN_250,Y,10.100.10.12,255.255.255.0,192.168.172.12,Y
TestVM02,192.168.30.11,LUNPRDVM02,60,6,24,PG_VLAN_250,N,10.100.10.13,255.255.255.0,N,N
My desired notification mail :
VMName OS DOMAIN LAN IP BACKUP IP VMState TotalCPU Totalmemory vCenter VMHost Hard disk 2 Hard disk 3
TestVM01 TestVM01 TRUE 10.100.10.12 192.168.172.12 PoweredON 4 16 192.168.100.10 192.168.30.10 50GB not defined
TestVM02 TestVM02 WORKGROUP 10.100.10.13 not defined PoweredON 6 24 192.168.100.10 192.168.30.11 60GB 500GB

The tricky part is where you get the disk sizes in GB from Read-Host.
I would probably maximize the number of disks that can be added and ensure the given value the user typed in is in fact an int.
Something like this:
# set a maximum number of disks to add in this demo no more than 10
$maxDisks = 10
$VCServer = Read-Host "Enter the vCenter server name"
$report = Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture -PipelineVariable row | ForEach-Object {
New-Vm .... blah blah
$newVM = [PSCustomObject]#{
'VMName' = $row.ServerName
'OS' = $row.ServerName
'DOMAIN' = if ($row.DOMAIN -eq 'Y') { 'TRUE' } else { 'WORKGROUP' }
'LAN IP' = $row.LANIP
'BACKUP IP' = if ($row.BACKUPIP -eq 'N') { 'not defined' } else { $row.BACKUPIP }
'VMState' = (Get-VM -Name $row.ServerName).summary.runtime.powerState
'TotalCPU' = $row.NumCPU
'Totalmemory' = $row.MemoryGB
'vCenter' = $VCServer
'VMHost' = $row.ESXHOST
}
# add the harddisk properties to the object, initialize to 'not defined'
for ($i = 1; $i -le $maxDisks; $i++) {
$newVM | Add-Member -MemberType NoteProperty -Name "Hard disk $i" -Value 'not defined'
}
# now ask for the disk sizes
$diskNumber = 1
$intSize = 0 # a variable to use in TryParse()
while ($diskNumber -le $maxDisks) {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if ($diskSize -eq 'no') { break } # exit the loop
if ([int]::TryParse($diskSize, [ref]$intSize)) {
New-HardDisk -VM $row.ServerName -CapacityGB $intSize -Confirm:$false | Out-Null
# update the value in the $newVM object
$diskItem = "Hard disk $diskNumber" # the property name in the object
$newVM.$diskItem = '{0}GB' -f $intSize
$diskNumber++ # increment the disk number
}
}
# output the completed object to be collected in the $report variable
$newVM
}
UPDATE
As per your comment you do not want to maximize the number of disks added beforehand, you can change the code to simply keep track of the disks added and afterwards 'fill in the blanks' so you will end up with objects having the same number of properties.
$VCServer = Read-Host "Enter the vCenter server name"
# counter to keep track of the maximum number of disks added
$maxDisks = 0
$report = Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture | ForEach-Object {
Write-Host "Creating new VM: $($_.ServerName)_" -ForegroundColor Yellow
New-Vm .... blah blah
$newVM = [PSCustomObject]#{
'VMName' = $_.ServerName
'OS' = $_.ServerName
'DOMAIN' = if ($_.DOMAIN -eq 'Y') { 'TRUE' } else { 'WORKGROUP' }
'LAN IP' = $_.LANIP
'BACKUP IP' = if ($_.BACKUPIP -eq 'N') { 'not defined' } else { $_.BACKUPIP }
'VMState' = (Get-VM -Name $_.ServerName).summary.runtime.powerState
'TotalCPU' = $_.NumCPU
'Totalmemory' = $_.MemoryGB
'vCenter' = $VCServer
'VMHost' = $_.ESXHOST
}
# ask for the disk sizes and create new disks
$diskNumber = 0
$intSize = 0 # a variable to use in TryParse()
while ($true) {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if ($diskSize -eq 'no') { break } # exit the loop
if ([int]::TryParse($diskSize, [ref]$intSize)) {
New-HardDisk -VM $_.ServerName -CapacityGB $intSize -Confirm:$false | Out-Null
# update the value in the $newVM object
$diskNumber++ # increment the disk number
$diskItem = "Hard disk $diskNumber" # the property name in the object
$newVM | Add-Member -MemberType NoteProperty -Name $diskItem -Value ('{0}GB' -f $intSize)
}
}
# update the $maxDisks variable
$maxDisks = [Math]::Max($diskNumber, $maxDisks)
# output the completed object to be collected in the $report variable
$newVM
}
# Your $report variable now holds a collection of VMs with a variable number of properties.
# To be able to create a decent table out of this, you need to add hardisk properties with value 'not defined' where needed.
foreach ($vm in $report) {
$diskCount = ($vm | Select-Object -Property 'Hard disk*').Count
for ($i = $diskCount; $i -lt $maxDisks; $i++) {
$vm | Add-Member -MemberType NoteProperty -Name "Hard disk $($i + 1)" -Value 'not defined'
}
}
# output on screen
$report
After this you can send the report via email, either as plain text or HTML table, exactly the same as in your previous question

I think you're asking how to output $Report as a string for the -body parameter.
You can do so like this:
$body = ($Report | Out-String)

Related

List of all Windows VMs under all subscriptions under a single Tenant

I want to write a script (preferably PowerShell calling the Az cli modules) that lists of all Windows VMs (whether allocated or de-allocated), subscription, resource-group, vm-name, the os system, the OS version, latest patch information under all Azure subscriptions under a single Azure Tenant. Want this in table format.
There are a number of scripts on the internet but use the 'old' Az modules.
I need help in identifying the Az modules to use and fields to query.
My high level pseudo logic.
$tenant_id = xxxx-xxxx-xxxx-xxxx
$subscription_array = Get-All-Subcriptions -tenant $tenant_id
for(each $sub in $subscription_array)
{
$vm_array = Get-Windows-Vms -subscription $sub
for(each $vm in $vm_array)
{
$vm_name = $vm.name
$vm_os = $vm.os -- Windows-2019, Windows_server-2012 etc...
$vm_rg = $vm.resource_group
$vm_patch_version = $vm.latest_patch_no
$collection_of_vm_records.add($vm_name, $vm_os, $vm_rg, $vm_patch_version)
}
}
Write $collection_of_vm_records to csv file
This will give you both windows and linux vm's details.
$report = #()
$subs = Get-AzSubscription
Foreach ($sub in $subs)
{
select-AzSubscription $sub | Out-Null
$subName = $sub.Name
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress
$nics = Get-AzNetworkInterface | Where-Object { $_.VirtualMachine -NE $null}
foreach ($nic in $nics) {
$info = "" | Select-Object VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
foreach($publicIp in $publicIps) {
if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
$info.PublicIPAddress = $publicIp.ipaddress
}
}
[string]$sku = $vm.StorageProfile.ImageReference.Sku
[string]$os = $vm.StorageProfile.ImageReference.Offer
$osDiskName = $vm.StorageProfile.OsDisk.Name
$info.VMName = $vm.Name
$info.OsType = $os + " " + $sku
$info.ResourceGroupName = $vm.ResourceGroupName
$info.Region = $vm.Location
$vmLocation = $vm.location
$info.VmSize = $vm.HardwareProfile.VmSize
$info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3]
$info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
$info.Subscription = $subName
if ($vmLocation)
{
$sizeDetails = Get-AzVMSize -Location $vmLocation | Where-Object {$_.Name -eq $vm.HardwareProfile.VmSize}
}
$info.Cores = $sizeDetails.NumberOfCores
$info.Memory = $sizeDetails.MemoryInMB
$osDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $osDiskName
$info.CreatedDate = $osDisk.TimeCreated
$report+=$info
}
}
$csv = $report | Select-Object VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate
$csv | Export-Csv C:\temp\Inventory.csv -NoTypeInformation

Timestamp of finished Powershell task

I have a basic script, which will shutdown Windows services and generate a report about their shutdown processes. I also want to include two more columns into my output variable ($table), which will be timestamp values i.e. when shutdown tasks were launched and when they finished. I have no idea how to implement this into my report.
$processlist = #('SQLTELEMETRY$TESTDB', 'MSSQL$TESTDB', 'SQLWRITER')
$get = ''
$table = #{ }
$failed = 0
foreach ($proc in $processlist) {
stop-service -name $proc -force
}
#start-sleep -s 120
foreach ($proc in $processlist) {
$get = get-service $proc -Erroraction ignore
if ($get.Status -eq 'Running') {
$table += #{$proc = 'Running' }
}
else {
$table += #{$proc = 'Stopped' }
}
}
foreach ($value in $table.GetEnumerator()) {
if ($value.Value -eq 'Running') {
$failed += 1
}
}
if ($failed -gt 0) {
$err = 'FAILED'
}
else {
$err = 'SUCCESS'
}
$table.GetEnumerator() | Select-Object -Property Name, Value | export-csv appreport.csv -delimiter ";" -force -notypeinformation
(HTML part here...)
Instead of adding stuff into a Hashtable, I think it would be a lot easier to build an array of objects and write that as CSV file.
Something like this:
$serviceList = 'SQLTELEMETRY$TESTDB', 'MSSQL$TESTDB', 'SQLWRITER'
$maxAttempts = 10
# $result will become an array of PsCustomObjects you can easily pipe to Export-Csv
$result = foreach ($service in $serviceList) {
$shutStart = Get-Date
$svc = Get-Service -Name $service -ErrorAction SilentlyContinue
if ($svc) {
for ($attempt = 0; $attempt -lt $maxAttempts; $attempt++) {
$shutResult = 'Failed'
Start-Sleep -Milliseconds 100
$svc | Stop-Service -Force -ErrorAction SilentlyContinue
# test if the service has stopped. If so exit the loop
if (($svc | Get-Service).Status -eq 'Stopped') {
$shutResult = 'Success'
break
}
}
[PsCustomObject]#{
'ServiceName' = $svc.Name
'ServiceDisplayName' = $svc.DisplayName
'ShutDownStart' = $shutStart
'ShutDownEnd' = Get-Date
'Result' = $shutResult
}
}
else {
[PsCustomObject]#{
'ServiceName' = $service
'ServiceDisplayName' = ''
'ShutDownStart' = $shutStart
'ShutDownEnd' = Get-Date
'Result' = "Failed: Service '$service' could not be found."
}
}
}
# output on screen
$result
# output to CSV
$result | Export-Csv 'D:\appreport.csv' -Delimiter ";" -Force -NoTypeInformation
The output on screen will look like this:
ServiceName : SQLTELEMETRY$TESTDB
ServiceDisplayName :
ShutDownStart : 22-8-2019 16:47:40
ShutDownEnd : 22-8-2019 16:47:40
Result : Failed: Service 'SQLTELEMETRY$TESTDB' could not be found.
ServiceName : MSSQL$TESTDB
ServiceDisplayName :
ShutDownStart : 22-8-2019 16:47:40
ShutDownEnd : 22-8-2019 16:47:40
Result : Failed: Service 'MSSQL$TESTDB' could not be found.
ServiceName : SQLWRITER
ServiceDisplayName : SQL Server VSS Writer
ShutDownStart : 22-8-2019 16:47:38
ShutDownEnd : 22-8-2019 16:47:39
Result : Success
Hope that helps
I don't really know when you want to capture the time stamp for the services, but I suggest you take advantage of the below property and add it in the loop where you think its suitable.
(Get-Process -Name $proc).StartTime
Also you can use the below properties :
UserProcessorTime
TotalProcessTime
ExitTime
I hope this will help you to capture to time.

Creating a Powershell Script to get disk info of remote machines

I have been playing with a script i found online that gives you a breakdown of your disks and a lovely pie chart after.
I was wondering this; we currently have VPN running to all our customer sites where there the servers are.
I can get to them all using the local IP for administration purposes via RDCM or Web Browser. I was wondering if there is anyway for me to be able to run the shell command directly from my machine and listing the ip's of the machines i want checked.
I tried using a list file of the ip's but it just returns nothing.
It would save a lot of time rather than clicking through all 100 servers to get the info every month.
Things to bare in mind:
Each site has it's own domain and admin account/password to access.
Here is the script which is direct from the technet script centre (I havent placed any details in.) It works fine when i run it directly on the machine):
#### Spreadsheet Location
$DirectoryToSaveTo = "c:\"
$date=Get-Date -format "yyyy-MM-d"
$Filename="serverinfo-$($date)"
$FromEmail="<ToEmail>"
$ToEmail="<FromEmail>"
$SMTPMail="<SMTP MAIL>"
###InputLocation
$Computers = Get-Content "c:\server.txt"
# before we do anything else, are we likely to be able to save the file?
# if the directory doesn't exist, then create it
if (!(Test-Path -path "$DirectoryToSaveTo")) #create it if not existing
{
New-Item "$DirectoryToSaveTo" -type directory | out-null
}
#Create a new Excel object using COM
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$sheet.Name = 'Server Inventory'
#Create a Title for the first worksheet
$row = 1
$Column = 1
$Sheet.Cells.Item($row,$column)= 'Server Inventory'
$range = $Sheet.Range("a1","s2")
$range.Merge() | Out-Null
$range.VerticalAlignment = -4160
#Give it a nice Style so it stands out
$range.Style = 'Title'
#Increment row for next set of data
$row++;$row++
#Save the initial row so it can be used later to create a border
#Counter variable for rows
$intRow = $row
$xlOpenXMLWorkbook=[int]51
#Read thru the contents of the SQL_Servers.txt file
$Sheet.Cells.Item($intRow,1) ="Name"
$Sheet.Cells.Item($intRow,2) ="status"
$Sheet.Cells.Item($intRow,3) ="OS"
$Sheet.Cells.Item($intRow,4) ="Domain Role"
$Sheet.Cells.Item($intRow,5) ="ProcessorName"
$Sheet.Cells.Item($intRow,6) ="Manufacturer"
$Sheet.Cells.Item($intRow,7) ="Model"
$Sheet.Cells.Item($intRow,8) ="SystemType"
$Sheet.Cells.Item($intRow,9) ="Last Boot Time"
$Sheet.Cells.Item($intRow,10) ="Bios Version"
$Sheet.Cells.Item($intRow,11) ="CPU Info"
$Sheet.Cells.Item($intRow,12) ="NoOfProcessors"
$Sheet.Cells.Item($intRow,13) ="Total Physical Memory"
$Sheet.Cells.Item($intRow,14) ="Total Free Physical Memory"
$Sheet.Cells.Item($intRow,15) ="Total Virtual Memory"
$Sheet.Cells.Item($intRow,16) ="Total Free Virtual Memory"
$Sheet.Cells.Item($intRow,17) ="Disk Info"
$Sheet.Cells.Item($intRow,18) ="FQDN"
$Sheet.Cells.Item($intRow,19) ="IPAddress"
for ($col = 1; $col –le 19; $col++)
{
$Sheet.Cells.Item($intRow,$col).Font.Bold = $True
$Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = 48
$Sheet.Cells.Item($intRow,$col).Font.ColorIndex = 34
}
$intRow++
Function GetStatusCode
{
Param([int] $StatusCode)
switch($StatusCode)
{
0 {"Success"}
11001 {"Buffer Too Small"}
11002 {"Destination Net Unreachable"}
11003 {"Destination Host Unreachable"}
11004 {"Destination Protocol Unreachable"}
11005 {"Destination Port Unreachable"}
11006 {"No Resources"}
11007 {"Bad Option"}
11008 {"Hardware Error"}
11009 {"Packet Too Big"}
11010 {"Request Timed Out"}
11011 {"Bad Request"}
11012 {"Bad Route"}
11013 {"TimeToLive Expired Transit"}
11014 {"TimeToLive Expired Reassembly"}
11015 {"Parameter Problem"}
11016 {"Source Quench"}
11017 {"Option Too Big"}
11018 {"Bad Destination"}
11032 {"Negotiating IPSEC"}
11050 {"General Failure"}
default {"Failed"}
}
}
Function GetUpTime
{
param([string] $LastBootTime)
$Uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBootTime)
"Days: $($Uptime.Days); Hours: $($Uptime.Hours); Minutes: $($Uptime.Minutes); Seconds: $($Uptime.Seconds)"
}
foreach ($Computer in $Computers)
{
TRY {
$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer
$Bios = Get-WmiObject -Class Win32_BIOS -ComputerName $Computer
$sheetS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer
$sheetPU = Get-WmiObject -Class Win32_Processor -ComputerName $Computer
$drives = Get-WmiObject -ComputerName $Computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
$pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$Computer'"
$IPAddress=(Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}).ipaddress
$FQDN=[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
$OSRunning = $OS.caption + " " + $OS.OSArchitecture + " SP " + $OS.ServicePackMajorVersion
$NoOfProcessors=$sheetS.numberofProcessors
$name=$SheetPU|select name -First 1
$Manufacturer=$sheetS.Manufacturer
$Model=$sheetS.Model
$systemType=$sheetS.SystemType
$ProcessorName=$SheetPU|select name -First 1
$DomainRole = $sheetS.DomainRole
$TotalAvailMemory = $OS.totalvisiblememorysize/1kb
$TotalVirtualMemory = $OS.totalvirtualmemorysize/1kb
$TotalFreeMemory = $OS.FreePhysicalMemory/1kb
$TotalFreeVirtualMemory = $OS.FreeVirtualMemory/1kb
$TotalMem = "{0:N2}" -f $TotalAvailMemory
$TotalVirt = "{0:N2}" -f $TotalVirtualMemory
$FreeMem = "{0:N2}" -f $TotalFreeMemory
$FreeVirtMem = "{0:N2}" -f $TotalFreeVirtualMemory
$date = Get-Date
$uptime = $OS.ConvertToDateTime($OS.lastbootuptime)
$BiosVersion = $Bios.Manufacturer + " " + $Bios.SMBIOSBIOSVERSION + " " + $Bios.ConvertToDateTime($Bios.Releasedate)
$sheetPUInfo = $name.Name + " & has " + $sheetPU.NumberOfCores + " Cores & the FSB is " + $sheetPU.ExtClock + " Mhz"
$sheetPULOAD = $sheetPU.LoadPercentage
if($pingStatus.StatusCode -eq 0)
{
$Status = GetStatusCode( $pingStatus.StatusCode )
}
else
{
$Status = GetStatusCode( $pingStatus.StatusCode )
}
if (($DomainRole -eq "0") -or ($DomainRole -eq "1"))
{
$Role = "Work Station"
}
elseif (($DomainRole -eq "2") -or ($DomainRole -eq "3"))
{
$Role = "Member Server"
}
elseif (($DomainRole -eq "4") -or ($DomainRole -eq "5"))
{
$Role = "Domain Controller"
}
else
{
$Role = "Unknown"
}
}
CATCH
{
$pcnotfound = "true"
}
#### Pump Data to Excel
if ($pcnotfound -eq "true")
{
$sheet.Cells.Item($intRow, 1) = "PC Not Found"
}
else
{
$sheet.Cells.Item($intRow, 1) = $computer
$sheet.Cells.Item($intRow, 2) = $status
$sheet.Cells.Item($intRow, 3) = $OSRunning
$sheet.Cells.Item($intRow, 4) = $Role
$sheet.Cells.Item($intRow, 5) = $name.name
$Sheet.Cells.Item($intRow, 6) = $Manufacturer
$Sheet.Cells.Item($intRow, 7) = $Model
$Sheet.Cells.Item($intRow, 8) = $SystemType
$sheet.Cells.Item($intRow, 9) = $uptime
$sheet.Cells.Item($intRow, 10)= $BiosVersion
$sheet.Cells.Item($intRow, 11)= $sheetPUInfo
$sheet.Cells.Item($intRow, 12)=$NoOfProcessors
$sheet.Cells.Item($intRow, 13)= "$TotalMem MB"
$sheet.Cells.Item($intRow, 14)= "$FreeMem MB"
$sheet.Cells.Item($intRow, 15)= "$TotalVirt MB"
$sheet.Cells.Item($intRow, 16)= "$FreeVirtMem MB"
$sheet.Cells.Item($intRow, 19)=$IPAddress
$sheet.Cells.Item($intRow, 18)=$FQDN
$driveStr = ""
foreach($drive in $drives)
{
$size1 = $drive.size / 1GB
$size = "{0:N2}" -f $size1
$free1 = $drive.freespace / 1GB
$free = "{0:N2}" -f $free1
$freea = $free1 / $size1 * 100
$freeb = "{0:N2}" -f $freea
$ID = $drive.DeviceID
$driveStr += "$ID = Total Space: $size GB / Free Space: $free GB / Free (Percent): $freeb % ` "
}
$sheet.Cells.Item($intRow, 17) = $driveStr
}
$intRow = $intRow + 1
$pcnotfound = "false"
}
$erroractionpreference = “SilentlyContinue”
$Sheet.UsedRange.EntireColumn.AutoFit()
########################################333
$Sheet = $Excel.Worksheets.Item(2)
$sheet.Name = 'DiskSpace'
$Sheet.Activate() | Out-Null
#Create a Title for the first worksheet
$row = 1
$Column = 1
$Sheet.Cells.Item($row,$column)= 'Disk Space Information'
$range = $Sheet.Range("a1","h2")
$range.Merge() | Out-Null
$range.VerticalAlignment = -4160
#Give it a nice Style so it stands out
$range.Style = 'Title'
#Increment row for next set of data
$row++;$row++
#Save the initial row so it can be used later to create a border
$initalRow = $row
#Create a header for Disk Space Report; set each cell to Bold and add a background color
$Sheet.Cells.Item($row,$column)= 'Computername'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= 'DeviceID'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= 'VolumeName'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= 'TotalSizeGB'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= 'UsedSpaceGB'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= 'FreeSpaceGB'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= '%Free'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$Sheet.Cells.Item($row,$column)= 'State'
$Sheet.Cells.Item($row,$column).Interior.ColorIndex =48
$Sheet.Cells.Item($row,$column).Font.Bold=$True
#Set up a header filter
$headerRange = $Sheet.Range("a3","h3")
$headerRange.AutoFilter() | Out-Null
#Increment Row and reset Column back to first column
$row++
$Column = 1
$critical=0
$warning=0
$good=0
#Get the drives and filter out CD/DVD drives
foreach ($computer in $Computers)
{
$diskDrives = Get-WmiObject win32_LogicalDisk -Filter "DriveType='3'" -ComputerName $computer
#Process each disk in the collection and write to spreadsheet
ForEach ($disk in $diskDrives) {
$Sheet.Cells.Item($row,1)= $disk.__Server
$Sheet.Cells.Item($row,2)= $disk.DeviceID
$Sheet.Cells.Item($row,3)= $disk.VolumeName
$Sheet.Cells.Item($row,4)= [math]::Round(($disk.Size /1GB),2)
$Sheet.Cells.Item($row,5)= [math]::Round((($disk.Size - $disk.FreeSpace)/1GB),2)
$Sheet.Cells.Item($row,6)= [math]::Round(($disk.FreeSpace / 1GB),2)
$Sheet.Cells.Item($row,7)= ("{0:P}" -f ($disk.FreeSpace / $disk.Size))
#Determine if disk needs to be flagged for warning or critical alert
If ($disk.FreeSpace -lt 5GB -AND ("{0:P}" -f ($disk.FreeSpace / $disk.Size)) -lt 40) {
$Sheet.Cells.Item($row,8) = "Critical"
$critical++
#Check to see if space is near empty and use appropriate background colors
$range = $Sheet.Range(("A{0}" -f $row),("H{0}" -f $row))
$range.Select() | Out-Null
#Critical threshold
$range.Interior.ColorIndex = 3
} ElseIf ($disk.FreeSpace -lt 10GB -AND ("{0:P}" -f ($disk.FreeSpace / $disk.Size)) -lt 60) {
$Sheet.Cells.Item($row,8) = "Warning"
$range = $Sheet.Range(("A{0}" -f $row),("H{0}" -f $row))
$range.Select() | Out-Null
$warning++
$range.Interior.ColorIndex = 6
} Else {
$Sheet.Cells.Item($row,8) = "Good"
$good++
}
$row++
}
}
#Add a border for data cells
$row--
$dataRange = $Sheet.Range(("A{0}" -f $initalRow),("H{0}" -f $row))
7..12 | ForEach {
$dataRange.Borders.Item($_).LineStyle = 1
$dataRange.Borders.Item($_).Weight = 2
}
#Auto fit everything so it looks better
$usedRange = $Sheet.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null
$critical
$warning
$good
$sheet = $excel.Worksheets.Item(2)
$row++;$row++
$beginChartRow = $Row
$Sheet.Cells.Item($row,$Column) = 'Critical'
$Column++
$Sheet.Cells.Item($row,$Column) = 'Warning'
$Column++
$Sheet.Cells.Item($row,$Column) = 'Good'
$Column = 1
$row++
#Critical formula
$Sheet.Cells.Item($row,$Column)=$critical
$Column++
#Warning formula
$Sheet.Cells.Item($row,$Column)=$warning
$Column++
#Good formula
$Sheet.Cells.Item($row,$Column)= $good
$endChartRow = $row
$chartRange = $Sheet.Range(("A{0}" -f $beginChartRow),("C{0}" -f $endChartRow))
##Add a chart to the workbook
#Open a sheet for charts
$temp = $sheet.Charts.Add()
$temp.Delete()
$chart = $sheet.Shapes.AddChart().Chart
$sheet.Activate()
#Configure the chart
##Use a 3D Pie Chart
$chart.ChartType = 70
$chart.Elevation = 40
#Give it some color
$sheet.Shapes.Item("Chart 1").Fill.ForeColor.TintAndShade = .34
$sheet.Shapes.Item("Chart 1").Fill.ForeColor.ObjectThemeColor = 5
$sheet.Shapes.Item("Chart 1").Fill.BackColor.TintAndShade = .765
$sheet.Shapes.Item("Chart 1").Fill.ForeColor.ObjectThemeColor = 5
$sheet.Shapes.Item("Chart 1").Fill.TwoColorGradient(1,1)
#Set the location of the chart
$sheet.Shapes.Item("Chart 1").Placement = 3
$sheet.Shapes.Item("Chart 1").Top = 30
$sheet.Shapes.Item("Chart 1").Left = 600
$chart.SetSourceData($chartRange)
$chart.HasTitle = $True
$chart.ApplyLayout(6,69)
$chart.ChartTitle.Text = "Disk Space Report"
$chart.ChartStyle = 26
$chart.PlotVisibleOnly = $False
$chart.SeriesCollection(1).DataLabels().ShowValue = $True
$chart.SeriesCollection(1).DataLabels().Separator = ("{0}" -f [char]10)
$chart.SeriesCollection(1).DataLabels().Position = 2
#Critical
$chart.SeriesCollection(1).Points(1).Format.Fill.ForeColor.RGB = 255
#Warning
$chart.SeriesCollection(1).Points(2).Format.Fill.ForeColor.RGB = 65535
#Good
$chart.SeriesCollection(1).Points(3).Format.Fill.ForeColor.RGB = 5287936
#Hide the data
$chartRange.EntireRow.Hidden = $True
$sheet.Name = 'DiskInformation'
$filename = "$DirectoryToSaveTo$filename.xlsx"
if (test-path $filename ) { rm $filename } #delete the file if it already exists
$Sheet.UsedRange.EntireColumn.AutoFit()
$Excel.SaveAs($filename, $xlOpenXMLWorkbook) #save as an XML Workbook (xslx)
$Excel.Saved = $True
$Excel.Close()
$Excel.DisplayAlerts = $False
$Excel.quit()
Function sendEmail([string]$emailFrom, [string]$emailTo, [string]$subject,[string]$body,[string]$smtpServer,[string]$filePath)
{
#initate message
$email = New-Object System.Net.Mail.MailMessage
$email.From = $emailFrom
$email.To.Add($emailTo)
$email.Subject = $subject
$email.Body = $body
# initiate email attachment
$emailAttach = New-Object System.Net.Mail.Attachment $filePath
$email.Attachments.Add($emailAttach)
#initiate sending email
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)
}
#Call Function
#sendEmail -emailFrom pjayaram#appvion.com -emailTo 'pjayaram#appvion.com,pthangaraj#appvion.com,rmurugesan#appvion.com' -subject "INVENTORY" -body "Windows Server Inventory - COMPLETE DETAILS" -smtpServer hqmail01.appletonpapers.com -filePath $filename
$message = #"
Hi Team,
The Discovery of Windows Server and Disk Space information for all the listed instances.
Autogenerated Email!!! Please do not reply.
Thank you,
Appvion.com
"#
$date=get-date
sendEmail -emailFrom $fromEmail -emailTo $ToEmail -subject "Windows Server Inventory & Disk Details -$($date)" -body $message -smtpServer $SMTPMail -filePath $filename
Many thanks to you all in advance.
For each computer in an external domain, you will have to get the corresponding credentials at the beginning with, for example :
$storedCredential = Get-Credential
Then, when making WMI calls, you will need to specify these credentials, with for example :
$OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -Credential $storedCredential
$Bios = Get-WmiObject -Class Win32_BIOS -ComputerName $Computer -Credential $storedCredential
$sheetS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer -Credential $storedCredential

Write-Host vs Write-Output - Newlines

I am having difficulty outputting a variable on the same line as a string of text. It works when I use Write-Host, but not Write-Output. I want to use Write-Output because it is seemingly the best practice (keeping things in the pipeline), but Write-Output always outputs the variable on a newline.
The line of code that I am having trouble with is:
Write-Host $VM "- VM name not valid: check the input file for spelling errors."
Also, I am fairly new to PowerShell scripting so I welcome any other input on this script.
Function Get-VMSwapping {
#requires –version 3.0
<#
.SYNOPSIS
Outputs the following: Cluster, VMHost, VM, SwappedMemory, BalloonedMemory.
.DESCRIPTION
Outputs the following: Cluster, VMHost, VM, SwappedMemory, BalloonedMemory.
Make sure to be connected to the vCenter server that has all of the VMs to enumerate.
.NOTES
Author: Nick Sousa
Date: Jan 28, 2014
Version: 1.0
#>
[CmdletBinding(
HelpURI='https://www.vmware.com/support/developer/PowerCLI/',
SupportsShouldProcess=$true,
ConfirmImpact="High"
)]
Param(
[ValidateScript({
If(Test-Path -Path $_) {$true}
Else {Throw "Input filename is not valid."}
})]
[Parameter(Mandatory=$false, ParameterSetName="InputFile",Position = 0)][string]$InputFile
)
Begin {
$ErrorActionPreference = "Stop"
} # End "Begin"
Process {
Try {
If($PSCmdlet.ShouldProcess("VMs that are ballooning & swapping")) {
If($PSBoundParameters.ContainsKey('InputFile')) {
$VMReport = #()
# Loop through each item in the file specified in -InputFile.
# If the VM cannot be validated with Get-VM, write an error to the screen.
# If the VM is found within vCenter, output its Swap/Balloon amounts.
ForEach($VM in Get-Content -Path $InputFile) {
If(Get-VM -Name $VM -ErrorAction SilentlyContinue) {
$VMFull = Get-VM -Name $VM
$VMName = $VMFull.Name
$VMView = Get-View -ViewType VirtualMachine -Filter #{"Name" = "^$VMName$"}
$obj = "" | Select-Object Cluster, VMHost, VM, SwappedMemory, BalloonedMemory
$obj.Cluster = $VMFull.Host.Parent.Name
$obj.VMHost = $VMFull.Host.Name
$obj.VM = $VMView.Name
$obj.SwappedMemory = $VMView.Summary.QuickStats.SwappedMemory
$obj.BalloonedMemory = $VMView.Summary.QuickStats.BalloonedMemory
$VMReport += $obj
} # End "If(Get-VM -Name $VM)"
Else {
Write-Host $VM "- VM name not valid: check the input file for spelling errors."
} # End Else for "If(Get-VM -Name $VM)"
} # End "ForEach($VM in $VMs)"
$VMReport | Format-Table -AutoSize
} # End "If($PSBoundParameters.ContainsKey('InputFile'))"
Else {
# Pull a list of all VMs that have Balloon Memory greater than 0.
# Loop through all VMs found and prepare a collection object to be outputted.
$VMReport = #()
$VMs = Get-View -ViewType VirtualMachine | Where-Object {$_.Summary.QuickStats.BalloonedMemory -ne "0"}
ForEach($VM in $VMs) {
$VMFull = Get-VM -Name $VM.Name
$obj = "" | Select-Object Cluster, VMHost, VM, SwappedMemory, BalloonedMemory
$obj.Cluster = $VMFull.Host.Parent.Name
$obj.VMHost = $VMFull.Host.Name
$obj.VM = $VM.Name
$obj.SwappedMemory = $vm.Summary.QuickStats.SwappedMemory
$obj.BalloonedMemory = $vm.Summary.QuickStats.BalloonedMemory
$VMReport += $obj
} # End "ForEach($VM in $VMs)"
$VMReport | Format-Table -AutoSize
} # End Else for "If($PSBoundParameters.ContainsKey('InputFile'))"
} # End "If($PSCmdlet.ShouldProcess("VMs that are ballooning & swapping"))"
} # End "Try"
Catch {
Write-Output "Caught an exception:"
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
} # End "Catch"
Finally {
$ErrorActionPreference = "Continue"
} # End "Finally"
} # End "Process"
} # End "Function Get-VMSwapping"
Concatenate the string
Write-Output ($VM + "- VM name not valid: check the input file for spelling errors.")

How do I find the Collection Membership Information with PowerShell

I have the following code, but I get an "Invalid Namespace" error constantly, I am pretty sure I am entering the correct information.
If you have another way of doing this, it's also accepted.
Param(
$SiteCode,
$SourceFile,
$Destination = "$env:USERPROFILE\DESKTOP\Computers.csv"
)
$Computers = Get-Content $SourceFile
$EmptyArray = #()
foreach($computer in $computers)
{
$ResourceIDQuery = Get-WmiObject -Namespace "root\sms\site_$SiteCode" -Class SMS_R_SYSTEM -Filter "Name='$computer'"
$CollectionQuery = Get-WmiObject -Namespace "root\sms\site_$SiteCode" -Class SMS_CollectionMember_a -filter "ResourceID='$($ResourceIDQuery.ResourceId)'"
foreach($Item in $CollectionQuery)
{
$DObject = New-Object PSObject
$Dobject | Add-Member -MemberType NoteProperty -Name "Computer" -Value $computer
$Dobject | Add-Member -MemberType NoteProperty -Name "ResID" -Value $($ResourceIDQuery.ResourceId)
$Dobject | Add-Member -MemberType NoteProperty -Name "CollID" -Value $($Item.CollectionId)
$Dobject | Add-Member -MemberType NoteProperty -Name "DirectOrNot" -Value $($Item.IsDirect)
$EmptyArray += $Dobject
}
}
$EmptyArray | ConvertTo-Csv -NoTypeInformation | Out-File $Destination
Rather than connecting to each computer and extracting the information (slow) get it from the straight from the database....
[CmdletBinding()]
param (
[string] $hosts = "",
[string] $sccmsrv = "",
[Parameter(Mandatory=$False,Position=3)]
[string] $path = ""
)
$usage = "USAGE: List-AdvertCollMembershipSCCM.ps1 -sccmsrv SCCMSERVER -hosts 'host1 host2 host3' -path 'c:\temp\Outfile.csv'"
if ($host -and $sccmsrv){
Write-Host ""
Write-Host -ForegroundColor Yellow "SCCM Server: $sccmsrv"
Write-Host -ForegroundColor Yellow "Looking at hosts: $hosts"
#### Function for executing a SQL query with integrated authentication
function execSQLQuery ([string]$fSQLServer, [string]$db, [string]$query){
$objConnection = New-Object System.Data.SqlClient.SqlConnection
$objConnection.ConnectionString = "Server = $fSQLServer; Database = $db; trusted_connection=true;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand $query, $objConnection
trap {Write-Host -ForegroundColor 'red' "($sqlsrv/$db not accessible)";continue}
$SqlCmd.Connection.Open()
if ($SqlCmd.Connection.State -ine 'Open') {
$SqlCmd.Connection.Close()
return
}
$dr = $SqlCmd.ExecuteReader()
#get the data
$dt = new-object "System.Data.DataTable"
$dt.Load($dr)
$SqlCmd.Connection.Close()
$dr.Close()
$dr.Dispose()
$objConnection.Close()
return $dt
}
# read the SCCM site name of the SCCM site server
$site = (gwmi -ComputerName $sccmsrv -Namespace root\sms -Class SMS_ProviderLocation).sitecode
# enumerating SQL server name for the given SCCM site server
$sccmCompquery = gwmi -q "Select distinct SiteSystem, Role, SiteCode FROM SMS_SiteSystemSummarizer where role = 'SMS SQL Server' and siteCode = '$site' ORDER BY SiteCode" -namespace "root\sms\site_$site" -ComputerName $sccmsrv
[string]$tmpstr = [regex]::Match($sccmCompquery.sitesystem, "\\\\\w+\\$")
$sccmSQLServer = $tmpstr.replace("\", "")
$objColl = #()
#### Collate the host list.
$hostlist = #($Input)
if ($hosts) {
if($hosts -imatch " "){
$hostsArr = #($hosts.split(" "))
$hostlist += $hostsArr
}
else{
$hostlist += $hosts
}
}
# going through the list of hosts
foreach($srv in $hostlist){
$memberQuery = "SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name', dbo.v_Collection.CollectionID, dbo.v_FullCollectionMembership.IsDirect "
$memberQuery += "FROM dbo.v_FullCollectionMembership INNER JOIN dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID "
$memberQuery += "WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('$srv'))"
# running sql query to enumerate list of collections the computer is member of
$membership = execSQLQuery $sccmSQLServer "SMS_$site" $memberQuery
# if we have a result, go through it and build an object collection with the computer name and the collection(s) it is member of
if($membership){
foreach($enumColl in $membership){
$sObject = $enumColl | select Hostname, ResourceID, "Collection Name", CollectionID, IsDirect
$objColl +=$sObject
}
}
}
if ($objColl){
if ($path){
$objColl | ft -AutoSize
Write-Host -ForegroundColor Yellow "Exporting to results to: $path"
$objColl | Export-Csv $path -NoTypeInformation
}
else{
$objColl | ft -AutoSize
Write-Host -ForegroundColor Green "Use the -path argument in the command line to export output to csv to display"
Write-Host -ForegroundColor Green "the 'IsDirect' Information"
Write-Host ""
}
}
Else {
foreach ($Hostname in $hostlist){
Write-Host ""
Write-Host -ForegroundColor Yellow "The host $hostname is not a member of any collection"
}
Write-Host -ForegroundColor Yellow "Check you have entered the correct hostname and try again"
}
}
else {
Write-Host ""
Write-Host -ForegroundColor Yellow $usage
}
Execution:-
PS C:\> ListSCCMCollections.ps1 -sccmsrv SCCMSERVER -hosts host1,host2,host3 -path "c:\temp\Outfile.csv"
or
PS C:\> Get-Content hostlist.txt | ListSCCMCollections.ps1 -sccmsrv SCCMSERVER -path c:\temp\Outfile.csv
Getting the requested info straight from SQL:-
SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name', dbo.v_Collection.CollectionID,
dbo.v_FullCollectionMembership.IsDirect
FROM dbo.v_FullCollectionMembership INNER JOIN
dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN
dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID
WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('Hostname'))
This script can be used with any SQL query on the SCCM database. All you need to do is update the SQL query in the script. i.e. the $memberQuery array (if you spread the query over a couple of lines like below, be sure to leave a space at the end of each line with exception to the last).
For example; If you'd like the script to show the clients collections with live advertisements assigned to them change the SQL query in the $memberQuery array to:-
$memberQuery = "SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name',dbo.v_Collection.CollectionID, dbo.v_FullCollectionMembership.IsDirect, dbo.v_Advertisement.AdvertisementID, dbo.v_Advertisement.AdvertisementName "
$memberQuery += "FROM dbo.v_FullCollectionMembership INNER JOIN dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID INNER JOIN dbo.v_Advertisement ON dbo.v_Collection.CollectionID = dbo.v_Advertisement.CollectionID "
$memberQuery += "WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('$srv'))"
and the $sObject variable to:-
$sObject = $enumColl | select Hostname, ResourceID, "Collection Name", CollectionID, IsDirect, AdvertisementID, AdvertisementName
Complete script to view client collections with live advisements (execution the same as before):-
[CmdletBinding()]
param (
[string] $hosts = "",
[string] $sccmsrv = "",
[Parameter(Mandatory=$False,Position=3)]
[string] $path = ""
)
$usage = "USAGE: List-AdvertCollMembershipSCCM.ps1 -sccmsrv SCCMSERVER -hosts 'host1 host2 host3' -path 'c:\temp\Outfile.csv'"
if ($host -and $sccmsrv){
Write-Host ""
Write-Host -ForegroundColor Yellow "SCCM Server: $sccmsrv"
Write-Host -ForegroundColor Yellow "Looking at hosts: $hosts"
#### Function for executing a SQL query with integrated authentication
function execSQLQuery ([string]$fSQLServer, [string]$db, [string]$query){
$objConnection = New-Object System.Data.SqlClient.SqlConnection
$objConnection.ConnectionString = "Server = $fSQLServer; Database = $db; trusted_connection=true;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand $query, $objConnection
trap {Write-Host -ForegroundColor 'red' "($sqlsrv/$db not accessible)";continue}
$SqlCmd.Connection.Open()
if ($SqlCmd.Connection.State -ine 'Open') {
$SqlCmd.Connection.Close()
return
}
$dr = $SqlCmd.ExecuteReader()
#get the data
$dt = new-object "System.Data.DataTable"
$dt.Load($dr)
$SqlCmd.Connection.Close()
$dr.Close()
$dr.Dispose()
$objConnection.Close()
return $dt
}
# read the SCCM site name of the SCCM site server
$site = (gwmi -ComputerName $sccmsrv -Namespace root\sms -Class SMS_ProviderLocation).sitecode
# enumerating SQL server name for the given SCCM site server
$sccmCompquery = gwmi -q "Select distinct SiteSystem, Role, SiteCode FROM SMS_SiteSystemSummarizer where role = 'SMS SQL Server' and siteCode = '$site' ORDER BY SiteCode" -namespace "root\sms\site_$site" -ComputerName $sccmsrv
[string]$tmpstr = [regex]::Match($sccmCompquery.sitesystem, "\\\\\w+\\$")
$sccmSQLServer = $tmpstr.replace("\", "")
$objColl = #()
#### Collate the host list.
$hostlist = #($Input)
if ($hosts) {
if($hosts -imatch " "){
$hostsArr = #($hosts.split(" "))
$hostlist += $hostsArr
}
else{
$hostlist += $hosts
}
}
# going through the list of hosts
foreach($srv in $hostlist){
$memberQuery = "SELECT dbo.v_FullCollectionMembership.Name AS 'Hostname', dbo.v_GS_SYSTEM.ResourceID, dbo.v_Collection.Name AS 'Collection Name',dbo.v_Collection.CollectionID, dbo.v_FullCollectionMembership.IsDirect, dbo.v_Advertisement.AdvertisementID, dbo.v_Advertisement.AdvertisementName "
$memberQuery += "FROM dbo.v_FullCollectionMembership INNER JOIN dbo.v_Collection ON dbo.v_FullCollectionMembership.CollectionID = dbo.v_Collection.CollectionID INNER JOIN dbo.v_GS_SYSTEM ON dbo.v_FullCollectionMembership.ResourceID = dbo.v_GS_SYSTEM.ResourceID INNER JOIN dbo.v_Advertisement ON dbo.v_Collection.CollectionID = dbo.v_Advertisement.CollectionID "
$memberQuery += "WHERE (LOWER(dbo.v_FullCollectionMembership.Name) = LOWER('$srv'))"
# running sql query to enumerate list of collections the computer is member of
$membership = execSQLQuery $sccmSQLServer "SMS_$site" $memberQuery
# if we have a result, go through it and build an object collection with the computer name and the collection(s) it is member of
if($membership){
foreach($enumColl in $membership){
$sObject = $enumColl | select Hostname, ResourceID, "Collection Name", CollectionID, IsDirect, AdvertisementID, AdvertisementName
$objColl +=$sObject
}
}
}
if ($objColl){
if ($path){
$objColl | ft -AutoSize
Write-Host -ForegroundColor Yellow "Exporting to results to: $path"
$objColl | Export-Csv $path -NoTypeInformation
}
else{
$objColl | ft -AutoSize
Write-Host -ForegroundColor Green "Use the -path argument in the command line to export output to csv to display"
Write-Host -ForegroundColor Green "the header 'AdvertisementName'"
Write-Host ""
}
}
Else {
foreach ($Hostname in $hostlist){
Write-Host ""
Write-Host -ForegroundColor Yellow "The host $hostname is not a member of any collection with live advertisements"
}
Write-Host -ForegroundColor Yellow "Check you have entered the correct hostname and try again"
}
}
else {
Write-Host ""
Write-Host -ForegroundColor Yellow $usage
}
And if you connect to SCCM from station with only SCCM Console installed and every other SCCM cmdlets works, trying Get-WmiObject:
Get-WmiObject -Namespace "root\sms\site_$SiteCode" ...
you will have an error:
“Get-WmiObject : Invalid namespace …”
In this situation you should specify parameter ComputerName and point to server where SCCM is installed:
Get-WmiObject -ComputerName "SCCMserver" -Namespace "root\sms\site_$SiteCode" ...
Hope it helps, I waste few minutes by this.