How do I find the Collection Membership Information with PowerShell - 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.

Related

List SPN's Script - Write results to file issue

Good morning everyone.
I found this script on the InterWeb's which works phenominal. HOWEVER... No matter what I try, and where I put it, I can't seem to get it to do the results to an out-file.
What the hell am I doing wrong, and where does the variable need to go?
# Source / credit:
# https://social.technet.microsoft.com/wiki/contents/articles/18996.active-directory-powershell-script-to-list-all-spns-used.aspx
cls
$search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
$search.filter = "(servicePrincipalName=*)"
## You can use this to filter for OU's:
## $results = $search.Findall() | ?{ $_.path -like '*OU=whatever,DC=whatever,DC=whatever*' }
$results = $search.Findall()
foreach( $result in $results ) {
$userEntry = $result.GetDirectoryEntry()
Write-host "Object Name = " $userEntry.name -backgroundcolor "yellow" -foregroundcolor "black"
Write-host "DN = " $userEntry.distinguishedName
Write-host "Object Cat. = " $userEntry.objectCategory
Write-host "servicePrincipalNames"
$i=1
foreach( $SPN in $userEntry.servicePrincipalName ) {
Write-host "SPN ${i} =$SPN"
$i+=1
}
Write-host ""
}

How to send notification mail via 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)

run and handle output from ps1 script

The goal : get all logged in / logged out users from the system.
Those users who logged in / logged out by using remote desktop connection.
My script :
Param(
[array]$ServersToQuery = (hostname),
[datetime]$StartTime = "January 1, 1970"
)
foreach ($Server in $ServersToQuery) {
$LogFilter = #{
LogName = 'Microsoft-Windows-TerminalServices-LocalSessionManager/Operational'
ID = 21, 23, 24, 25
StartTime = $StartTime
}
$AllEntries = Get-WinEvent -FilterHashtable $LogFilter -ComputerName $Server
$AllEntries | Foreach {
$entry = [xml]$_.ToXml()
[array]$Output += New-Object PSObject -Property #{
TimeCreated = $_.TimeCreated
User = $entry.Event.UserData.EventXML.User
IPAddress = $entry.Event.UserData.EventXML.Address
EventID = $entry.Event.System.EventID
ServerName = $Server
}
}
}
$FilteredOutput += $Output | Select TimeCreated, User, ServerName, IPAddress, #{Name='Action';Expression={
if ($_.EventID -eq '21'){"logon"}
if ($_.EventID -eq '22'){"Shell start"}
if ($_.EventID -eq '23'){"logoff"}
if ($_.EventID -eq '24'){"disconnected"}
if ($_.EventID -eq '25'){"reconnection"}
}
}
$Date = (Get-Date -Format s) -replace ":", "."
$FilePath = "$env:USERPROFILE\Desktop\$Date`_RDP_Report.csv"
$FilteredOutput | Sort TimeCreated | Export-Csv $FilePath -NoTypeInformation
Write-host "Writing File: $FilePath" -ForegroundColor Cyan
Write-host "Done!" -ForegroundColor Cyan
#End
I really do not understand ps1 scripts. I've found this script but i want to use it for my purposes.
When i try to execute it with c# :
Scenario 1 :
string scriptText = "C:\\MyPath\\script.ps1";
try
{
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
Collection<PSObject> results = pipeline.Invoke();
It throws an error :
ps1 is not digitally signed.
Second scenario :
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript(str_Path);
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
if (PowerShellInstance.Streams.Error.Count > 0)
{
// error records were written to the error stream.
// do something with the items found.
}
}
Streams are always empty. Actually it always count 0 rows.
Any idea/suggestion how to get this done ?
I used Write-Output instead of Write-Host into the end of the script without generating csv file. Credits to this.

Powershell workflow/parallel citrix sessions

Edit - I should preface this by saying I'm very new to powershell. This is one of the first "complicated" scripts i've tried to write.
I'm trying to use workflow/parallel to run a script against two lists simultaneously. after I get it working, the goal will be to add multiple lists, to cut down on the time it takes to process each one.
The script by itself works great. Adding the workflow returns nothing. :/
$Global:ctxsession = Read-Host -Prompt 'Input the Username'
$serverlist1 = get-content .\serverlist1.txt
$serverlist2 = get-content .\serverlist2.txt
function citrixlist {
#([string]$Servers)
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$servers
)
foreach ($server in $servers) {
#ping first
if (Test-Connection -count 1 -computer $server -quiet) {
#temporary write-host to see where it hangs (might leave in permanently)
Write-Host $server
#check for sessions
$sessions = qwinsta /server $server| ?{ $_ -notmatch '^ SESSIONNAME' } | %{
#separating into objects
$item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
$item.Active = $_.Substring(0,1) -match '>'
$item.SessionName = $_.Substring(1,18).Trim()
$item.Username = $_.Substring(19,20).Trim()
$item.Id = $_.Substring(39,9).Trim()
$item.State = $_.Substring(48,8).Trim()
$item.Type = $_.Substring(56,12).Trim()
$item.Device = $_.Substring(68).Trim()
$item
}
foreach ($session in $sessions){
#match for session name entered
if ($session.Username -match $ctxsession){
Write-Host 'Found' $session.Username 'on' $server 'with Session ID' $session.Id $session.State
#Kill session with ID and Server, with verbose switch
#rwinsta $session.Id /server:$server /v
}
}
}
}
}
workflow get-citrixlist1 {
citrixlist -servers $serverlist1
}
workflow get-citrixlist2 {
citrixlist -servers $serverlist2
}
workflow get-citrixkill {
parallel {
get-citrixlist1
get-citrixlist2
}
}

Import-vApp throwin Line 157: Duplicate element 'AddressOnParent' error

I am trying to deploy an OVA file using powershell script. I found a script from internet and modified it a little bit and execute. During execution at Import-VApp command i am seeing this error: Import-VApp Line 157: Duplicate element 'AddressOnParent'.
I am able to deploy the OVA manually. But thru this script, i am seeing this error. Any help would be appreciated.
Please find my powershell script below. (keeping only important script below, per suggestion).
param(
)
#---- Custom OVA Properties ::> START <:: ----- #
$vAPPPropertyId_ip4enable = "IPv4 Enable"
$vAPP_ip4enable = "True"
$vAPPPropertyId_ip4address = "IPv4 Address"
$vAPP_ip4address = "10.51.98.20"
$vAPPPropertyId_ip4netmask = "IPv4 Netmask"
$vAPP_ip4netmask = "255.255.255.0"
$vAPPPropertyId_ip4gateway = "Ipv4 Gateway"
$vAPP_ip4gateway = "10.51.98.1"
$vAPPPropertyId_ip6enable = "Ipv6 Enable"
$vAPP_ip6enable = "False"
#$vAPPPropertyId_ip6address = "IPv6 Address"
#$vAPP_ip6address = ""
#$vAPPPropertyId_ip6gateway = "IPv6 Gateway"
#$vAPP_ip6gateway = ""
$vAPP_defaultRouteKey = 1
$vAPP_defaultRouteCmd = ""
#---- Custom OVA Properties ::> END <:: ----- #
$rp = Get-ResourcePool -name $resourcePool
Write-Host " Resoure Pool = $rp"
$cluster = $rp.ExtensionData.Owner
$vhost = (Get-Cluster -Id $cluster).ExtensionData.Host[0]
Write-Host " VHost = $vhost "
$VMHost = Get-VMHost -Id $vhost
Write-Host " VMHost = $VMHost "
if ($PortProfile -ne "")
{$PortgroupId = Get-VirtualPortGroup -Name $PortProfile -Distributed -VMHost (Get-VMHost $VMHost)}
Write-host "PortGroup = $PortgroupId"
Write-Host "Importing VApp .... "
$ovalocation = "C:\Script\s42700x8_1_1_a2.ova"
Write-Host "vAppName = $vAppName"
$vms = Import-VApp -Source $ovalocation -Name $vAppName -VMHost $VMHost #-Location $rp -Datastore $ds
#$vms = Import-VApp -Source $ovalocation -Name $vAppName -Location $rp -VMHost $VMHost -Datastore $ds
}
finally {
disconnect-VIServer -Confirm:$false
}
Output looks like this:
PS C:\Users\Administrator> C:\Script\OvaTestFile2.ps1
9/22/2014 3:23:58 AM Import-VApp Line 157: Duplicate element 'AddressOnParent'.