powershell Get-ChildItem iis:\sites | Export_CliXml sites.xml doesn't result in same as string output - iis

Short:
Get-ChildItem IIS:\Sites
Get-ChildItem IIS:\Sites | Get-CliXml sites.xml
returns one thing as string and another as xml. Why?
Longer:
When I run
Get-ChildItem IIS:\Sites
I get something in the line of
Name ID State Phys...
Default Web Site 1 Started C:\i...
MyTestSite 2 Started C:\i...
but when I pipe it to Export-CliXml like so:
Get-ChildItem IIS:\Sites | Get-CliXml sites.xml
I get nothing with "Default Web Site" or "MyTestSite" in it. => The string and xml don't have anything in common.
When I do the same with Get-Process
Get-Process | Export-CliXml processes.xml
I can find my processes in the output file. => What is in the string can be found in the xml.
Why does Get-Process output about the same as text and xml
while Get-ChildItems IIS:\Sites one text and something else as xml?
What is it I have misunderstood?
I guess a problem might be that I get an exception from Get-ChildItem iis:\sites but output is still generated. The xml output seems ok too.
Update:
When I do
Get-ChildItem IIS:\Sites | select Name,ID,State,PhysicalPath | Get-CliXml sites.xml
I get the values I am looking for.
When I add Bindings
Get-ChildItem IIS:\Sites | select Name,ID,State,PhysicalPath,Bindings | Get-CliXml sites.xml
only 2 sites are iterated and I get an error message. The xml is fine anyway. (some well constructed try-catch in the code i believe)

Can someone confirm or deny that
get-childitem iis:\sites
gives back an object with lots of objects (not strings and integers)
and when this is piped to
export-clixml
the code will do a ToString on every property and return type names for them.
If the above is the case (which it probably is) the solution is to
Get-ChildItem IIS:\Sites | select Name,ID,State,PhysicalPath | Get-CliXml sites.xml
but not for complex properties like Binding.

Related

Looking to validate that certain string is present in a text file, send warning if not

I have a process where files containing data are generated in separate locations, saved to a networked location, and merged into a single file.
And the end of the process, I would like to check that all locations are present in that merged file, and notify me if not.
I am having a problem finding a way to identify that a string specific to each location isn't present, to be used in an if statement, but it doesn't seem to be identifying the string correctly?
I have tried :
get-childitem -filter *daily.csv.ready \\x.x.x.x\data\* -recurse | where-object {$_ -notin 'D,KPI,KPI,1,'}
I know it's probably easier to do nothing if it is present, and perform the warning action if not, but I'm curious if this can be done in the reverse.
Thank you,
As Doug Maurer points out, your command does not search through the content of the files output by the Get-ChildItem command, because what that cmdlet emits are System.IO.FileInfo (or, potentially, System.IO.DirectoryInfo) instances containing metadata about the matching files (directories) rather than their content.
In other words: the automatic $_ variable in your Where-Object command refers to an object describing a file rather than its content.
However, you can pipe System.IO.FileInfo instances to the Select-String cmdlet, which indeed searches the input files' content:
Get-ChildItem -Filter *daily.csv.ready \\x.x.x.x\data\* -Recurse |
Where-Object { $_ | Select-String -Quiet -NotMatch 'D,KPI,KPI,1,' }

Delete rows in a .CSV file containing specific character with Powershell

I receive an automatic weekly export from a system in a .csv format. It contains a lot of usernames with the initials of the users (e.g. "fl", "nk"). A few of them have their first and last names, separated by coma (e.g. firstname.lastname). These are the ones, which have to be deleted from the .csv file.
My goal here is to write a Powershell script, which delete all rows, containing the character "." (dot) and then save the same .csv file by overwritting it.
Since I'm very new to Powershell, I'd highly appreciate a more detailed answer including the potential code. I tried various examples from similar issues, which I found here, but none of them worked and/or I am getting error messages, mostly because my syntax isn't correct.
Additional info. Here is a part of the table.
I tried this code:
Get-Content "D:\file.csv" | Where-Object {$_ -notmatch '\.'} | Set-Content "D:\File.csv"-Force -NoTypeInformation
As Mathias says, it is helpful to see what you have tried so we can help you come to a working result. It is easy to give you something like this:
$csv = Import-Csv -Path C:\Temp\temp.csv -Delimiter ";"
$newCSV = #()
foreach($row in $csv){
if(!$row.username -or $row.username -notlike "*.*"){
$newCSV += $row
}
}
$newCSV | Export-Csv -Path C:\Temp\temp.csv -Delimiter ";" -NoTypeInformation
The above code eliminates rows that have a dot on the username field. It leaves rows with an empty username intact with the 'if(!$row.username' part. But I have no idea whether this is helpful since there is no example CSV file, also there is no way to know what you have tried so far ;)
Note that I always prefer using ";" as delimiter, because opening the file in Excel will already be correctly seperated. If the current file uses ',' as a delimiter, you will need to change that when importing the CSV.
You were very close! For this you don't need a loop, you just need to do it using the correct cmdlets:
(Import-Csv -Path 'D:\file.csv' -Delimiter ';') |
Where-Object { $_.Initials -notmatch '\.' } |
Export-Csv -Path 'D:\file.csv' -Delimiter ';' -Force -NoTypeInformation
Get-Content simply reads a text file and returns the lines as string array, whereas Import-Csv parses the structure and creates objects with properties from the header line.
The brackets around the Import-Csv are needed to ensure the importing/parsing of the file is completely done before piping the results through. Without that, the resulting file may become completely empty because you cannot read and overwrite the same file at the same time.

Powershell: How to get the location of a file, depending on its name?

So my task is to write a PS script, that outputs the location of a database file. The location of the file is:
C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\Mailbox database Name\Mailbox database Name.edb
I figured I can get the name of my Exchange database with
Get-MailboxDatabase | fl Name
which has the output:
Mailbox Database 0161713049
which is the name of the db but there is a bunch of invisible characters before and after the actual name.
So my question is, how could I get rid of these invisible characters? I want to concat a string to make it look like this:
C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\Mailbox Database 0161713049\Mailbox Database 0161713049.edb
I would need this code to work on servers with completely different database names too, so simply removing the unwanted characters from the start with .Remove() may help, but since I don't know for sure the length of the name of the database, I can't remove the characters at the end.
Also I can't get rid of the feeling that there is a much simpler way to get the location of my .edb file.
Powershell treats almost all outputs as an object with properties in hashtable format like #{Name=MYEXCHDB}. When you just want a property value as a string instead, you must expand it like #AdminOfThings suggests:
Get-MailboxDatabase | Select-Object -ExpandProperty Name
To concatenate the name into a string:
$myString = "C:\path\to\$(Get-MailboxDatabase | Select-Object -ExpandProperty Name)"
And as #mathias-r-jessen suggests, the path to the database is another property you can get directly:
Get-MailboxDatabase | Select-Object -ExpandProperty EdbFilePath | Select-Object -ExpandProperty PathName

PowerShell - Export-CSV only listing last of Text File

This may be really ridiculous and simple but I am missing something. I have a very basic script block, but the output is doing something funky. Code Block is:
Get-Content C:\test.txt | ForEach-Object{(Get-ADComputer -Identity $_ -Properties description) | Select-Object name, description `
| Export-Csv -Path 'C:\test.csv' -NoTypeInformation -Force}
The strange thing is that if I comment out the export-csv cmdlet, the code works perfectly by grabbing everything in the text file and lists all the descriptions (as it should). However, when i include it, the Export only lists the very last item on the text file. I have read a few other questions similar to this but no success. Like I said, I am sure it is something simple but I can't seem to find it.
Thank You!
I found it.. i was missing -append in the export-csv and that worked.

replace string if you dont' know rest of string in PowerShell

Please help. Trying to figure out how to replace a string in PowerShell, but don't know the rest of the string. I have this:
(Get-Content $file) -replace[regex]::Escape('file='*''),('file='+$_.BaseName) | Set-Content $file
I don't know what comes after file=
I tried my code, but it replaces it multiple times instead of just once.
So trying to replace file=* with filename=$_.BaseName.
Thanks for looking.
Just an FYI for anyone using the latest version of PowerShell Community Extensions (http://pscx.codeplex.com), there is a new command called Edit-File that handles this sort of thing nicely (works hard to preserve the file's original encoding):
Get-Item test.txt | Foreach {$bn=$_.BaseName; $_} |
Edit-File -Pattern '(file=).*' -Replace "`${1}$bn"
In theory I shouldn't need the Foreach stage but it seems I've found a limitation in how -PipelineVariable does not work with parameters that aren't pipeline bound. Hmm, add that to the Pscx backlog.

Resources