|
Judge Schnoopy posted:Can somebody here shed some light on the proper way to do this? code:
|
# ? Oct 12, 2018 01:46 |
|
|
# ? May 28, 2024 15:00 |
|
Judge Schnoopy posted:Can somebody here shed some light on the proper way to do this? code:
code:
Edit: also these aren't pscustomobjects, they're just hashtables. Edit 2: Ignoring the syntax and assuming you do have a list of pscustomobjects, you can use "$item | Get-Member -MemberType NoteProperty" to see what properties it has code:
mystes fucked around with this message at 02:04 on Oct 12, 2018 |
# ? Oct 12, 2018 01:49 |
|
Even with all that PowerShell won't throw an exception if the value of your object property is null, so performing an evaluation of the value (real or non-existent) is fine as-is. Simply code:
Something PowerShell does that a lot of other languages don't is allow you to evaluate the existence of value from a variable in an If statement, so you can key conditions off a specific variable without typing as much (i.e. "variable.value == null", etc.). It's handy and for your purpose if you want to note whether a property exists and add other conditional behaviors on that it's pretty easy to do.
|
# ? Oct 12, 2018 02:31 |
|
PierreTheMime posted:Even with all that PowerShell won't throw an exception if the value of your object property is null, so performing an evaluation of the value (real or non-existent) is fine as-is.
|
# ? Oct 12, 2018 02:39 |
|
mystes posted:I recommend using strict mode (in which case this does produce an error) and writing the tiny amount of extra code to make sure stuff actually exists. Writing powershell code with strict mode off is a recipe for disaster. We use strict mode and that's where the trouble lies. I can't even do a if($thing.a) to check, because B will throw an exception for not having property a. I was phone-posting so no the syntax is not correct, I was just trying to convey the difference in properties and how I'm trying to compare them. I also found out that throwing $null is idiotic as gently caress because guess what happens when the last condition throws an exception? That's right, my collection of matches gets wiped away by the throw{$null}. Duh. Get-member sounds like it's going to care of my problem, thanks! e; AHA! I'm either smarter than I know or luckier than I am smart. At some point I added a 'type' property to each custom object in case I needed that info later. And now I can just do If($thing.type -eq 'ThingWithA') and If($Thing.type -eq 'ThingWithB') instead. Judge Schnoopy fucked around with this message at 03:10 on Oct 12, 2018 |
# ? Oct 12, 2018 02:58 |
|
Judge Schnoopy posted:Can somebody here shed some light on the proper way to do this?
|
# ? Oct 12, 2018 03:25 |
|
Judge Schnoopy posted:We use strict mode and that's where the trouble lies. I can't even do a if($thing.a) to check, because B will throw an exception for not having property a. You'll avoid a lot of grief if you make sure that arrays of objects contain objects of the same type and with the same members. If you need to combine properties from multiple sources then create new custom objects with the required properties. I find the easiest way is to just use New-Object and then Add-Member with the NotePropertyMembers parameter which accepts a hashtable of keys/values. Also Select-Object is really powerful and allows you to transform objects on the fly Name/Expression pairs. For example (Kind of lame but only one I could think of on the spot): code:
|
# ? Oct 12, 2018 05:40 |
|
I ended up refactoring my objects to have the same core properties, and use the same view XML. Now I can treat them all the same instead of making three separate cases. Each 'type' just has extraneous properties that are useful, but not required by the script for anything. Now I need to figure out how to retain order on items found in a JSON. Order matters, but my output seems to throw objects from all over the place. Nothing in the JSON object indicates order, which I guess is my downfall.
|
# ? Oct 12, 2018 15:31 |
|
Judge Schnoopy posted:Now I need to figure out how to retain order on items found in a JSON. Order matters, but my output seems to throw objects from all over the place. Nothing in the JSON object indicates order, which I guess is my downfall.
|
# ? Oct 12, 2018 15:46 |
|
Is there a way other than by twiddling around in the hosts file that I can add a DNS entry to a single Windows PC? (I basically need to add a DNS entry for a PaaS service, deploy to PaaS service, then remove the DNS entry -- boring red tape-y issues prevent this from being handled sanely for the time being)
|
# ? Oct 12, 2018 16:37 |
|
mystes posted:If you need to preserve the order of the keys I think you are going to need to use an OrderedDictionary rather than a PSCustomObject but I'm not sure JSON is really supposed to work that way. This project is super convoluted at this point, but the JSON isn't a custom object. The JSON is a bunch of nested hashtables in arrays in hashtables in arrays, from a completely separate source. My tool is trying to make sense of that JSON data by searching, building custom objects, and displaying nicely. The JSON Array is in the correct order, but my searching doesn't appear to keep that order when finding results. I'm not sure what step is jumbling the results. I think I'm going to just build in an 'Order' tag when I'm pulling from the primary source and building the JSON. Then I can just look at that property instead of making sure my search isn't loving up.
|
# ? Oct 12, 2018 16:56 |
|
New Yorp New Yorp posted:Is there a way other than by twiddling around in the hosts file that I can add a DNS entry to a single Windows PC?
|
# ? Oct 12, 2018 18:11 |
|
anthonypants posted:You either put it in the hosts file or you put it on a DNS resolver or you use the IP address instead of the hostname. That's what I thought. Can't use the IP directly, so I guess I'll either deal with it or build something to safely manipulate entries in the hosts file. Bummer.
|
# ? Oct 12, 2018 19:33 |
|
Judge Schnoopy posted:Now I need to figure out how to retain order on items found in a JSON. Order matters, but my output seems to throw objects from all over the place. Nothing in the JSON object indicates order, which I guess is my downfall. Why does order matter? Order should never matter for any key/value pair object be it dict, hashtable or JSON. If you can elaborate on why order somehow matters then maybe we can fix that issue instead. Also the thing I mentioned before about Select-Object? Well the array of names you pass to the Properties parameter are parsed in that exact order so you can actually re-order properties. For example: code:
New Yorp New Yorp posted:Is there a way other than by twiddling around in the hosts file that I can add a DNS entry to a single Windows PC? Nope. Hosts file is the only local fall back for name resolution on Windows, unless you go comedy-option and spin up a resolver on the client itself to temporarily resolve queries. Tell whoever is in charge of your local resolvers to stop being a dick.
|
# ? Oct 12, 2018 19:52 |
|
Pile Of Garbage posted:Why does order matter? Order should never matter for any key/value pair object be it dict, hashtable or JSON. If you can elaborate on why order somehow matters then maybe we can fix that issue instead. The source json is firewall rules so order most definitely matters. The hashtables are set into arrays in order in the json. Either way, my extraction tool is now tracking 'order number' so I can sort and resort and put back to normal whenever I want.
|
# ? Oct 13, 2018 00:25 |
Getting some odd stuff with SMA and hopefully someone has encountered this before. Briefly, I'm creating a ticket, applying a template to overwrite it, then pulling the object. Later on I'll be using the object to apply user relationships to it and description changes. What's annoying is that if I launch this as a job in SMA it works, if I run it through the ISE console with the SMA ISE add-on it works, but when I call this with another runbook it errors out with, "A parameter cannot be found that matches parameter name 'eq'. (A parameter cannot be found that matches parameter name 'eq'.)" It's breaking at: $SRProjection = Get-SCSMObjectProjection -ProjectionName $SRTypeProjection.Name -filter “ID -eq $SRID” @connection It seems to want a GUID for this filter but when running the get-scsmobjectprojections and selecting any of those objects, none have ID in GUID format (((( code:
Submarine Sandpaper fucked around with this message at 21:18 on Oct 26, 2018 |
|
# ? Oct 26, 2018 19:44 |
|
What makes you think it wants a GUID? Didn't know you could just put your filter values in quotes, I've always used curly braces and parentheses. Get-ADUser -Filter {(Title -eq 'CEO')}
|
# ? Oct 27, 2018 03:44 |
When I purposefully break it by putting an invalid ID and run it manually the exception is asking for a GUID. /E without writing the property ID, the ID that's generated for the exception just lacks SR so it's not even a guid. Submarine Sandpaper fucked around with this message at 14:46 on Oct 27, 2018 |
|
# ? Oct 27, 2018 14:44 |
|
Submarine Sandpaper posted:When I purposefully break it by putting an invalid ID and run it manually the exception is asking for a GUID. From the error it sounds like it doesn't like your filter. ""A parameter cannot be found that matches parameter name 'eq'. (A parameter cannot be found that matches parameter name 'eq'.)"" Can you try another filter format? Or provide the full body of relevant errors? $error[0] | select *
|
# ? Oct 27, 2018 18:09 |
PBS posted:From the error it sounds like it doesn't like your filter. writeErrorStream : True PSMessageDetails : Exception : Microsoft.PowerShell.Commands.WriteErrorException: A parameter cannot be found that matches parameter name 'eq'. TargetObject : CategoryInfo : InvalidData: ( [Write-Error], Error Creating Service Request FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Create-SCSMServiceRequest ErrorDetails : InvocationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at Create-SCSMServiceRequest, C:\ProgramData\Microsoft\System Center\Orchestrator\7.2\SMA\Sandbo xes\yteug04o.u1l\temp\srycaoqe.mpc\Create-SCSMServiceRequest.ps1: line 192 at <ScriptBlock>, C:\ProgramData\Microsoft\System Center\Orchestrator\7.2\SMA\Sandboxes\yteug04o .u1l\temp\srycaoqe.mpc\Create-SCSMServiceRequest.ps1: line 198 at <ScriptBlock>, <No file>: line 38 PipelineIterationInfo : {0, 0} /e- changed the filter syntax to {(logic)} and got: exception property (property) output writeErrorStream : True PSMessageDetails : Exception : Microsoft.PowerShell.Commands.WriteErrorException: property TargetObject : CategoryInfo : InvalidData: ( [Write-Error], Error Creating Service Request FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Create-SCSMServiceRequest ErrorDetails : InvocationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at Create-SCSMServiceRequest, C:\ProgramData\Microsoft\System Center\Orchestrator\7.2\SMA\Sandbo xes\yteug04o.u1l\temp\rpg2k2z4.dsv\Create-SCSMServiceRequest.ps1: line 192 at <ScriptBlock>, C:\ProgramData\Microsoft\System Center\Orchestrator\7.2\SMA\Sandboxes\yteug04o .u1l\temp\rpg2k2z4.dsv\Create-SCSMServiceRequest.ps1: line 198 at <ScriptBlock>, <No file>: line 38 PipelineIterationInfo : {0, 0} /e- first error when chaning ID to display name so it's not the guid Submarine Sandpaper fucked around with this message at 15:10 on Oct 29, 2018 |
|
# ? Oct 29, 2018 14:53 |
|
Submarine Sandpaper posted:I think this is everything code:
|
# ? Oct 29, 2018 16:28 |
anthonypants posted:Is this line 192 or 198? 192 is the error catch, 198 is the call to the create-scsmservicerequest function. That helped though, was (and I hope I'm done with this) able to fix it by: code:
|
|
# ? Oct 29, 2018 16:59 |
|
Submarine Sandpaper posted:192 is the error catch, 198 is the call to the create-scsmservicerequest function.
|
# ? Oct 29, 2018 17:12 |
|
anthonypants posted:That's weird, because single-quotes typically means what's inside doesn't get parsed. So that variable might not get expanded.
|
# ? Oct 29, 2018 17:23 |
|
New Yorp New Yorp posted:That's what I thought. Can't use the IP directly, so I guess I'll either deal with it or build something to safely manipulate entries in the hosts file. Bummer. do you happen to have dsc push or sccm?
|
# ? Oct 29, 2018 18:27 |
|
mystes posted:Are you talking about "ID -eq '$SRID'"? Putting single quotes inside a string won't magically prevent variables from getting expanded. Single quotes tells powershell to interpret the contents literally. PS C:\Users\PBS> $test = 123 PS C:\Users\PBS> Write-Output $test 123 PS C:\Users\PBS> Write-Output '$test' $test In the case of "ID -eq '$SRID'", that should be interpreted as, Does ID equal the string $SRID?
|
# ? Oct 30, 2018 01:50 |
|
PBS posted:Single quotes tells powershell to interpret the contents literally. This is what the original line in question was. code:
This means 1) the contents of this string are not even evaluated as powershell code. 2) Even they were actually later evaluated as powershell code that's irrelevant, because when the entire literal is evaluated, the variable $SRID will be substituted. Again, the presence of single quotes inside the string is irrelevant. E.g.: code:
|
# ? Oct 30, 2018 02:02 |
|
mystes posted:*snip* You're right, I didn't know single quotes behaved in that way within double quotes actually. Anyone know how it works that way?
|
# ? Oct 30, 2018 04:25 |
|
PBS posted:You're right, I didn't know single quotes behaved in that way within double quotes actually. Anyone know how it works that way?
|
# ? Oct 30, 2018 05:48 |
|
What is the proper syntax to switch "\\server\e$\" with "s3://bucketname/" ? I am writing a script that will parse a list of directory_path+filenames and will copy them to an S3 bucket. The problem is that the list is of windows files in the form of: \\server\e$\folder name\folder name2\foldername3\etc\file name.txt or some variant thereof (containing a server name, a drive share, a bunch of folders and then a file name) and the destination needs to be in the form of s3://bucket-name/folder name/folder name2/foldername3/etc/file name.txt What I'd planned on doing is reading the file name and doing a bunch of -replace strings to get the command syntax correct then do a aws cp $oldString $newString to get it copied. The problem is I cannot figure out the syntax of the replace command to switch "\\server\e$\" with "s3://bucketname/". I've tried single quotes to avoid having e$ getting parsed as regex and a few other things, but I can't get it working.
|
# ? Oct 31, 2018 02:05 |
|
Agrikk posted:What is the proper syntax to switch "\\server\e$\" with "s3://bucketname/" ? You either need to use [regex]::Escape(), properly escape the string by hand as a regular expression, or use another method. code:
Edit: Incidentally, if you don't want to use replace, maybe something like this will work for you: code:
mystes fucked around with this message at 02:48 on Oct 31, 2018 |
# ? Oct 31, 2018 02:25 |
|
Don't use the -replace operator, just use the Replace() method:code:
Pile Of Garbage fucked around with this message at 13:29 on Oct 31, 2018 |
# ? Oct 31, 2018 04:06 |
|
Thank you for these responses. My next question if the command I'm using is aws s3 cp $line $newline Where $line = \\server\e$\folder name\folder name2\foldername3\etc\file name.txt and $newline is s3://bucketname/folder name/folder name2/foldername3/etc/file name.txt How do I encapsulate the output of each string to account for the spaces in the file path? I need the final output to be: aws s3 cp "\\server\e$\folder name\folder name2\foldername3\etc\file name.txt" "s3://bucketname/folder name/folder name2/foldername3/etc/file name.txt"
|
# ? Oct 31, 2018 21:29 |
|
Agrikk posted:Thank you for these responses.
|
# ? Oct 31, 2018 21:33 |
|
anthonypants posted:aws s3 cp "$line" "$newline" That's not working. It's simply passing the line aws s3 cp "$line" "$newline" to the AWS CLI.
|
# ? Oct 31, 2018 21:57 |
aws s3 cp "`"$line`"" "`"$newline`"" those are the escapes you need if i'm reading you right. surround in more quotes if you're using it like a filter string. Submarine Sandpaper fucked around with this message at 01:05 on Nov 1, 2018 |
|
# ? Nov 1, 2018 00:58 |
|
I recently wrote up a script to migrate our on-prem distribution lists to O365 with the ultimate goal of eliminating our hybrid exchange server. As part of the script I wanted to be able to connect to on-prem exchange and O365 at the same time, so combined a couple different methods that I found and came up with this: code:
code:
code:
The Fool fucked around with this message at 21:51 on Nov 1, 2018 |
# ? Nov 1, 2018 19:44 |
|
I have what I hope is a fairly simple question. I'm brand new to powershell, so I might be missing something super obvious. I have a powershell script that connects to our exchange server from O365, passes credentials specific for one account, looks at every message and copies any attachments to a network location. This works fine. What I want it to do next is to build a text file with specific information from the email. So grab the sender name, email address, date created, date received, and the body of the message. I have it built so it is using an Invoke-RestMethod and points to the URL for Office 365 and then pulls the info for the matching message.ID. What I can't figure out is how to get it to pull the value of a field, rather than the whole thing. See my code below. code:
code:
code:
code:
CzarChasm fucked around with this message at 17:45 on Nov 5, 2018 |
# ? Nov 5, 2018 17:37 |
|
It depends on what object is being returned by Invoke-RestMethod. From the cmdlet's help page:quote:PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes. For JavaScript Object Notation (JSON) or XML, PowerShell converts (or deserializes) the content into objects. If the REST response is being properly deserialized then you should be able to use dot-notation to access the properties. Use the GetType() method to check what object(s) are being returned and pipe to Get-Member to discover properties: code:
|
# ? Nov 6, 2018 04:29 |
|
|
# ? May 28, 2024 15:00 |
|
CzarChasm posted:[snip] code:
|
# ? Nov 6, 2018 15:48 |