I am trying to schedule a list of URL's in maintenance mode in SCOM 2007 using powershell. I am trying to get the list of URLs display name from a text file and trying to pass as input to below command.However it's not working. Can some body help how to pass the display name in text file as input
$URLStuff = Get-Content C:\Display.txt
$URLWatcher = (Get-MonitoringClass -name Microsoft.SystemCenter.WebApplication.Perspective) |
Get-MonitoringObject | where {$_.DisplayName -eq $URLStuff}
get-content is returning you an array of string objects, one per line found in the file. You need to turn your where-object around to search that array for the DisplayName of each object found from SCOM.
$URLWatcher = (Get-MonitoringClass -name Microsoft.SystemCenter.WebApplication.Perspective) |
Get-MonitoringObject | where {$URLStuff -contains $_.DisplayName}
I'm assuming that you've already verified that DisplayName does contain the data you're looking for and will match the contents of Display.txt.
Related
How can I add the header to a top of text file using PowerShell? I have tried using the below code like:
Add-Content -Path E:\BHS_output_1.txt -Value (Get-Content "E:\header.txt")
Like this:
$textfile = 'E:\BHS_output_1.txt'
$headerfile = 'E:\header.txt'
$(Get-Content $headerfile; Get-Content $textfile) | Set-Content $textfile
Read the contents of header and text file after each other in a subexpression, then write the entire output back to the text file. The subexpression is required, so that reading from the text file is completed before writing starts, otherwise you'd get an access conflict.
I have a .URL file in my favorites like this:
[DEFAULT]
BASEURL=http://www.facebook.com/
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=https://31.13.74.144/
IDList=
IconFile=http://www.facebook.com/favicon.ico
IconIndex=1
I want to replace the IP address portion with the IP address of FB which I pull out in a Powershell script:
# set .url file
$outfile = "C:\Users\aborgetti\Favorites\facebook.url"
# set regex pattern to replace in url file
$regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
# grab the ping
$ping = New-Object System.Net.NetworkInformation.Ping
# grab the facebook IP ONLY
$ips = $($ping.Send("www.facebook.com").Address).IPAddressToString
# output to shell for test
GC $outfile| Where-Object { $_ -match "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"}| ForEach-Object {$_ -replace $ips}
I think I'm close, but my -match matches the entire line of the string and returns URL=https://31.13.74.144/
Would it be easier to store the results of GC $outfile| Where-Object { $_ -match "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"} to a variable and then do a substring?
Am I missing something? I know there's got to be an easier way to do this.
Thanks in advance!
You're using it wrong. You're searching for lines including an IP, and then trying to remove the new ip, which wasn't even in the text. -replace 'matchpattern, 'newvalue' is the syntax to replace text. -replace 'matchpattern' deletes the matched text.
What you want to do is:
Show all lines(Get-Content) -> Replace all IPs with new IP. Like:
GC $outfile| Foreach-Object { $_ -replace "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", $ips}
Any help?
(gc $outfile) -replace '^URL=https://[0-9.]+/',"URL=https://$((Test-Connection www.facebook.com -Count 1 ).IPV4Address)"
I am trying to get the following powershell script to output the results into a .csv. The .csv file is created but there is no data, what am I doing wrong?
$rootfolder = Get-ChildItem -recurse -Path [PATH] | where {$_.Attributes -eq 'Directory'}
foreach ($userdir in $rootfolder) {
$userdir.FullName
get-acl $userdir.FullName | foreach {write-host "The owner is : " $_.Owner -Foregroundcolor Green } | export-csv c:\powershell\test.csv
Write-Host "`n"
}
You can't send the output of a foreach further in the pipeline in the way you're attempting; you need to export each item individually. Revised script (note that I've also amended your where-object to test the PSIsContainer property of each item returned by gci):
$rootfolder = Get-ChildItem -recurse -Path [path] | where {$_.PSIsContainer}
foreach ($userdir in $rootfolder) {
get-acl -path $userdir.FullName | export-csv c:\powershell\test.csv -NoTypeInformation -Append
}
However, I think you'll be disappointed with the results. get-acl produces a collection of objects, not a collection of basic strings. For example, if I run get-acl on my profile directory, I get this:
PS C:\Users\ME> get-acl .|select-object access|fl *
Access : {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule,
System.Security.AccessControl.FileSystemAccessRule}
Running the above script, all I see in the Access column of the CSV is System.Security.AccessControl.AuthorizationRuleCollection - because that's what's produced, a collection. To get the full ACL, you will need to do additional processing to pull out each element of that collection. Repeat for the other fields that also return collections.
I need a way to update timestamps of files to the current date using a text document as a pointer to the files.
For example:
Directory A structure:
A:\Level 1\Level 2\somefile1
A:\Level 1\somefile2
A:\Level 1\somefile3
A:\somefile4
Text file (FilePointer.txt) contents:
A:\Level 1\Level 2\somefile1
A:\Level 1\somefile2
A:\Level 1\somefile3
A:\somefile4
Is there a way to parse the FilePointer.txt file to obtain file locations, and feed that to something that can update the timestamp for those files? Using powershell? Other methods?
That should be pretty easy with PowerShell:
$timestamp = Get-Date # now
Get-Content filepointer.txt | Get-Item | Foreach {$_.LastWriteTime = $timestamp}
There's a bit simpler approach outlined here. It uses the PowerShell Community Extensions Set-FileTime command e.g.:
Get-Content filepointer.txt | Get-Item | Set-FileTime
Or using aliases:
gc filepointer.txt | gi | touch
I have a command that formats it's output in the form of CSV. I have a list of machine this command will run against using a foreach loop. in the below example $serverlist is automatically generated with an AD Query.
foreach ($server in $serverlist) {
$outputlist = mycommand
}
what I would like to do is somehow end up with objects from the resulting CSV so I can then only select certain objects for a report. However the only way I can see to do this is using import-csv, which only seems to want to work with files and not variable: ie.
Import-Csv output.csv | ft "HostName","TaskName" |
Where-object {$_.TaskName -eq 'Blah'}
I'd like to be able to have import-csv $outputlist instead. doing this causes import-csv to have a hissyfit :)
Can anyone point me in the right direction on how to achieve this?
Cheers
The command you want is called ConvertFrom-CSV. The syntax is shown below.
NAME
ConvertFrom-CSV
SYNOPSIS
Converts object properties in comma-separated value (CSV) format into CSV
versions of the original objects.
SYNTAX
ConvertFrom-CSV [[-Delimiter] <char>] [-InputObject] <PSObject[]> [-Header <string[]>] [<CommonParameters>]
ConvertFrom-CSV -UseCulture [-InputObject] <PSObject[]> [-Header <string[]>] [<CommonParameters>]