r/PowerShell • • Feb 16 '20

Script Sharing Can we start a PowerShell project of the day?

Kind of like on r/dailyprogrammer

It would be really nice honestly and a way for all of us to get better at this craft. :)

I am also really curious to see how others would tackle things. So, for example if the project of the day (potd) was "Create a simple script that pulls inventory info then sends them via email in a csv file"

I don't know, hopefully the mods approve.

155 Upvotes

21 comments sorted by

29

u/gregnorz Feb 16 '20

4

u/[deleted] Feb 16 '20

This is so much fun, even to the veteran PowerShell user I could imagine it being a blast.

games are always fun. 🤷🏽‍♂️

6

u/RaimerRaimer Feb 16 '20

That looks super helpful for a rookie like me!

Thank you for sharing.

4

u/gregnorz Feb 16 '20 edited Feb 16 '20

Sure thing. I’ve only done a few of them, but they helped me wrap my head around PowerShell, coming from the server-side .NET world.

15

u/Sys_man Feb 16 '20

My powershell project of the the day is to watch New on this sub and try to help people.

4

u/Lee_Dailey [grin] Feb 16 '20

[grin]

1

u/jevans_ Feb 17 '20

Was going to say the same thi

10

u/Altheran Feb 16 '20

I got a nice one to start !

A PS module to convert any Kind of geographic coordinates to another. Maybe a port of https://proj.org/

How great it would be to input my list of NAD83 LCC XY coordinates and get a list of corresponding WSG84 lat/long...

Another one :

A module to transform a properly formatted CSV file to a nested JSON.

Like doing what this tool does : https://csv.keyangxiang.com/

(I managed to transform my vertical data into keyed columns in power query)

Then I upload manually, save the resulting JSON that I then input in my PS script (an invoke-restmethod POST)

11

u/anomalous_cowherd Feb 16 '20

Coordinate conversion is a horrible one, unless you are planning to use a library. It seems simple enough then you get into geoids and datums and non-unique representations and...

I've been hurt badly by that one before!

1

u/Altheran Feb 16 '20

Why i said "Maybe a port of https://proj.org/ "

There is a .NET port of proj.
https://github.com/NetTopologySuite/ProjNet4GeoAPI

1

u/anomalous_cowherd Feb 16 '20

Oh, ok. I thought you were proposing doing a complete Powershell port of it. Don't do that!

4

u/allywilson Feb 16 '20 edited Aug 12 '23

Moved to Lemmy (sopuli.xyz) -- mass edited with redact.dev

1

u/Altheran Feb 16 '20

From an excel table that translate to this CSV :

Entry_index,project.key,type.name,linksto,summary,steps.index,steps.step,steps.data,steps.result
1,PRJ,Test,PRJ-001;PRJ-002,Short Description 1,1,Do this,With this,It should give this
2,PRJ,Test,PRJ-003,Short Description abc,1,Do that,With these,This should happen
2,PRJ,Test,PRJ-003,Short Description abc,2,Then that,,Nothing should be displayed
2,PRJ,Test,PRJ-003,Short Description abc,3,Finally do this,Here is the info,Here is the expected end result
3,PRJ,Test,PRJ-001;PRJ-002;PRJ-003,Short Description about stuff,1,Do this step,,Error code xyz-001 should display
3,PRJ,Test,PRJ-001;PRJ-002;PRJ-003,Short Description about stuff,2,Then do this,Use this info,Success

Then Power Query munch it, I save the excel file.

I use import-excel to grab the new table and convert it to this csv result

fields.project.key,fields.issuetype.name,fields.summary,fields.linksto.0,fields.linksto.1,fields.linksto.2,fields.steps.0.index,fields.steps.0.step,fields.steps.0.data,fields.steps.0.result,fields.steps.1.index,fields.steps.1.step,fields.steps.1.data,fields.steps.1.result,fields.steps.2.index,fields.steps.2.step,fields.steps.2.data,fields.steps.2.result
PRJ,Test,Short Description 1,PRJ-001,PRJ-002,,1,Do this,With this,It should give this,,,,,,,,
PRJ,Test,Short Description abc,PRJ-003,,,1,Do that,With these,This should happen,2,Then that,,Nothing should be displayed,3,Finally do this,Here is the info,Here is the expected end result
PRJ,Test,Short Description about stuff,PRJ-001,PRJ-002,PRJ-003,1,Do this step,,Error code xyz-001 should display,2,Then do this,Use this info,Success,,,,

I then go to https://csv.keyangxiang.com/, check the "ignore empty" option, upload my csv file and convert it to this JSON :

[
  {
    "fields": {
      "project": {
        "key": "PRJ"
      },
      "issuetype": {
        "name": "Test"
      },
      "summary": "Short Description 1",
      "linksto": [
        "PRJ-001",
        "PRJ-002"
      ],
      "steps": [
        {
          "index": "1",
          "step": "Do this",
          "data": "With this",
          "result": "It should give this"
        }
      ]
    }
  },
  {
    "fields": {
      "project": {
        "key": "PRJ"
      },
      "issuetype": {
        "name": "Test"
      },
      "summary": "Short Description abc",
      "linksto": [
        "PRJ-003"
      ],
      "steps": [
        {
          "index": "1",
          "step": "Do that",
          "data": "With these",
          "result": "This should happen"
        },
        {
          "index": "2",
          "step": "Then that",
          "result": "Nothing should be displayed"
        },
        {
          "index": "3",
          "step": "Finally do this",
          "data": "Here is the info",
          "result": "Here is the expected end result"
        }
      ]
    }
  },
  {
    "fields": {
      "project": {
        "key": "PRJ"
      },
      "issuetype": {
        "name": "Test"
      },
      "summary": "Short Description about stuff",
      "linksto": [
        "PRJ-001",
        "PRJ-002",
        "PRJ-003"
      ],
      "steps": [
        {
          "index": "1",
          "step": "Do this step",
          "result": "Error code xyz-001 should display"
        },
        {
          "index": "2",
          "step": "Then do this",
          "data": "Use this info",
          "result": "Success"
        }
      ]
    }
  }
]

MUCH stronger than ConvertTo-Json

7

u/KBunn Feb 16 '20

Maybe a dedicated sub for that?

28

u/KingHill89 Feb 16 '20

r/DailyPowerShell perhaps. Using the same format as daily programmer

5

u/phasePup Feb 16 '20

Interested!

1

u/Titus_1024 Feb 17 '20

I was debating starting one but not sure I'd be up for running a subreddit

1

u/jiggle-o Feb 16 '20

Agreed! This would be useful for many, including me.

1

u/elevul Feb 16 '20

Wrote a script to copy software installer files from a local machine to all the local machines in the staging area so that we dont have to wait for Altiris' atrocious speed for new machines.

Since this script is to be used by all colleagues had to include a lot of input validation and error checking to sanitize inputs and make sure nothing unexpected happens. It ended up being a full page of code for a working code of basically one line.

Quite challenging since it was my first time dealing with that.

1

u/sundi712 May 13 '20

So whatever happened with this idea? I love it

0

u/[deleted] Feb 16 '20

[deleted]

2

u/Sys_man Feb 16 '20

so the trick here is being able to pass a variable in to the script block of an invoke-command, using the $Using:variablename (pay attention to the second instance of invoke-command):

$DomACred = Get-Credential     #A domain credentials
$DomBCred = Get-Credential     #B domain credentials

#get list of users in security group on domain A.
$AdomainUsers = invoke-command -computername <domainADC> -scriptblock {get-adgroupmember <groupName>} -credential $DomACred 

#foreach of those users, remove them from domain B.
foreach($user in $AdomainUsers){
    invoke-command -computername <domainBDC> -scriptblock {remove-aduser -Identity $Using:user} -credential $DomBCred 
}

The above will remove all users in AdomainGroup from domain B. Obviously, test this on a test domain, or with a safer command like get-aduser, rather than go straight to remove-aduser.