Hi wrote a very rudimentary script to check if a service is started and if not to start it. The start of the script imports a CSV file with server names in this format
servername
Server1
server2
server3
in my script, I have a foreach loop and when I call the variable I can specify $server.servername because the csv has a column for servername (Guessing here)
$servers = import-csv c:\folder\servers.csv
Foreach ($server in $servers)
{
$service = invoke-command -ComputerName $server.servername {Get-Service -Name 'PCNSSVC'}
Now I’m trying to automate the script even further by not specifying the csv but getting the information I want from more commands and building another server list. however, this server list does not have a property “servername” or any identifiable property. it looks like it is just a string of names and I cannot just append the previous script (that looks at the csv) to this script
#Firstly import the Active Directory Module, to gather a list of all the domain controllers
Import-Module ActiveDirectory
#Set the variables to gather the information
$ADForest = Get-ADForest
$ADForestDomainNamingMaster = $ADForest.DomainNamingMaster
$ADForestDomains = $ADForest.Domains
$ADForestSchemaMaster = $ADForest.SchemaMaster
$ADInfo = Get-ADDomain
$ADDomainDNSRoot = $ADInfo.DNSRoot
$ADDomainInfrastructureMaster = $ADInfo.InfrastructureMaster
$ADDomainPDCEmulator = $ADInfo.PDCEmulator
$ADDomainReadOnlyReplicaDirectoryServers = $ADInfo.ReadOnlyReplicaDirectoryServers
$ADDomainReplicaDirectoryServers = $ADInfo.ReplicaDirectoryServers
$ADDomainRIDMaster = $ADInfo.RIDMaster
Write-Verbose “Discovering Domain Controllers in the AD Forest $ADForestName `r “
ForEach ($Domain in $ADForestDomains)
{ ## OPEN ForEach Domain in ADForestDomains
$DomainDCs = Get-ADDomainController -filter * -server $Domain
ForEach ($DC in $DomainDCs)
{ ## OPEN ForEach DC in DomainDCs
$DCName = $DC.HostName
Write-Verbose “Adding $DCName to ForestDC list `r “
[array] $ForestDCs += $DC.HostName
} ## CLOSE ForEach DC in DomainDCs
} ## CLOSE ForEach Domain in ADForestDomains
$ForestDCsCount = $ForestDCs.count
Write-Verbose “Initial discovery found $ForestDCsCount DCs `r “
# Add all DC lists into $DomainControllers
$DomainControllers = $ForestDCs + $ADDomainReadOnlyReplicaDirectoryServers + $ADDomainReplicaDirectoryServers + $ADForestGlobalCatalogs
# Remove duplicate DCs from $DomainControllers
$DomainControllers = $DomainControllers | Select-Object -Unique
Foreach ($server in $DomainControllers)
....
I get an error that it cannot find the service on the server, but it doesn’t look at every server in the “string”
Is there a way to assign a property value to each of the servers so I can call it using $server.servername.
are all the servers individual objects, or is the variable one long string?
Sorry if I’m vague here, still learning