Powershell script formatting issue - powershell

When I run Get-Recipient | ft Name in the console everything looks good,
but when I try it in a script the output is just a list of class names;
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
How do I fix this? Thankful for any and all help!
//David

The Format-* cmdlets should only be used to control display of data. They should rarely if ever be used inside "library" type scripts or functions, expect maybe when displaying status messages or something. The actual output of a Format-* cmdlet is a bunch of magic objects (the FormatEntryData guys) that direct the Powershell engine on how to do display formatting.
It works in the console because you are not capturing your data, you are just letting it display to the screen.
In your script, you should just return the data as-is, and let the caller decide how to format it, if (s)he wants to. If you want to return back only the Name field, use Select-Object Name to pare away the other fields.

Related

Pulling data from and writing back to an Excel spreadsheet in PowerShell

Right, I'm putting a script together to be run post-Windows install to change a few settings, re-name the computer and join a domain. I've got everything working fine but where it currently asks the user to provide a computername (which they will be taking from a spreadsheet) I'd ideally like the script to take the next available name from the file and then write back a value for the adjacent field with a value provided by the user (the end-user's name).
Is this at all possible or am I expecting too much here?
Thanks in advance!
Check out the PowerShell Module https://www.powershellgallery.com/packages/ImportExcel/5.1.1

Logging Powershell Job Results

I have created a powershell interface which essentially collects information and then sends that info to an external PS script via Start-Job. I have a timer that triggers recieve-job every second, and updates an output box with STDOUT from the job I created.
External PS Script:
This script has many functions (which utilize many cmdlets), and will be printing messages via Write-Verbose to a log. The issue is, sometimes in these functions I print messages and/or also return a value.
As we all know, anything that a function returns is placed into standard output. So anytime a function returns a value, that value is printed into the Output Rich Textbox control. I don't want that. Also, if I am using VERBOSE, the message is not being printed in the control (however, it is being printed to the console)
What I want:
Print only messages to my rich text box AND my log file, with no function return values in the stream (unless they are apart of a message itself).
Note: I am re-writing this application from a VB.NET app, to a Powershell Studio app... and I noticed previously this was being done by streaming STD Output into the text box, using a similar external powershell script--except that script was using WRITE-HOST. That doesn't make sense to me, as write-host isn't even supposed to be put into the stream.
Alternative Approach:
I had a working example of letting the Job add to the log itself, and then having the UI run Get-Content -tail 1... This WOULD WORK, except that some of my messages will be printed on more than one line, and it wouldn't pull everything as expected. If anyone has a way for me to get any X number of new lines in the log at all times, let me know. That might be easier than my current approach.
so basically you want to use powershell backwards? functions should only return a value that you want in your actual output stream, anything that should not be returning a value to the stream should either be captured in a variable or sent to Out-Null, if want to provide information on what the command is doing that is what write-verbose is for. and Write-Verbose is working as intended, just like write-host it is not put into the stream.
To answer your question what you should do is rewrite your functions so that they return a single value(or no value at all if that is appropriate), for functions that cannot be rewritten you can suppress their output in your script by either assigning them to a variable(if they must be used later) or simply piping them to Out-Null. anything that is currently being captured in the verbose stream that needs to be a part of your output should also be moved to the standard stream and written with Write-Output, potentially with `If statements wrapped around them if you only want that data on certain runs, however if you really want to combine the verbose stream with the standard stream you can check out the info here - Redirect two or more Powershell streams other than output stream to the same file

Need PowerShell help! Very strange things are happening

So I'm using PowerShell to manipulate a SharePoint 2010 library. I am uploading, downloading, and deleting files in a script using a custom module I made. My errors are so odd I can't understand them.I am using PowerGUI, Windows PowerShell ISE, and PowerShell Management Shell all in admin mode.
PowerGUI:
I sometimes can't get an spWeb object, sometimes I can. The URL string is being pulled from a CSV file so it never changes and neither does the code before I call Get-SPWeb -Identity $correctURL
Sometimes when I call a list RootFolder it returns $false for the Exists property, using management shell I can get past this. Otherwise I can touch it by calling $ListName.RootFolder.Files and it will magically return and hold the $true for Exists in future executions of my script.
Then when I call an XML file full of file properties(for uploaded files) it will return file property names for $fileFieldsXML.row.Attributes | foreach {$_} and values for $fileFieldsXML.row.Attributes | foreach {$_.ToString()}. This is, unless I set them to variables. When two very distinct vars are set to these two differentish calls they both are set to the array of property names! Why??
Windows PowerShell ISE and PowerShell Management Shell
I think these are just outdated somehow. I can call Get-SPWeb in Management Shell but I can't in ISE due to I guess outdated versions. Lately the management shell will act as if I haven't been doing anything to the files unless I close it out and reopen it. Does the management shell just hold a copy of all files when it starts or something? Can I make it update these files?
Can anyone suggest a better way to debug? Also why does a module seem to severely increase runtime? When everything was in the same script it was quick but my long functions take several times longer to execute now.
I also have been using PowerShell and SharePoint for almost two months now, so I am a beginner and intern. Perhaps that is really the cause of my problems :)

Writing custom Powershell Cmdlet to accept table of data

I need to write a custom powershell cmdlet in C# that would accept data and be able to enumerate over it. I have followed some examples online and have some working code.
The problem I have can be demonstrated as follows:
I want a custom powershell cmdlet to take that output of another command as input to a parameter, so for example Get-Process
I want my custom cmdlet to accept data in table format
Enumerate over the data
The problem I have is that I am not sure how to setup a parameter to take a table of data in
Not sure how to output data from my cmdlet in table form.
Can anyone provide any examples?
I have no idea what you mean with "table format". In PowerShell you should work with objects (or, of course, lists of objects).
What you want to do, to create a cmdlet which takes the output of one command as it's input, i.e. accepts pipeline input, is to mark one of your parameter to take the value from the pipeline. You can find good instructions on how to do this at the MSDN section on Adding Parameters that Process Pipeline Input.

Get Current Drive Letter Using Powershell

All I'm looking for is a way to get the current drive letter that I am currently running the powershell script from.
I know I can get the current path using Get-Location but is there a way to extract the drive letter without doing some kind of substring operation?
Yes, you can get the drive letter without string operations:
(get-location).Drive.Name
Remember, that PowerShell seldom returns strings, but rich objects. You can always examine your further options from a result by using get-member.
Also note, that if you are not on the file system provider, the Name might not be a one-character string.
Edit:
As per x0n's comment below, here is a shorthand version:
$pwd.drive.name