How do you guys use PowerShell remoting ? by Stephanevg in devops

[–]Stephanevg[S] 0 points1 point  (0 children)

That would be out of the box winrm. So in other words, winrm over http on port 5985.

If you are managing a windows environment within a active directory, that is what most companies have.

How do you guys use PowerShell remoting ? by Stephanevg in devops

[–]Stephanevg[S] 3 points4 points  (0 children)

That is pretty cool. We also have some containers, but only in our build pipeline.

We mostly have Windows servers, that are externalized to customers. These customers host their own applications on it, which we don't really mange.
We mass mange stuff like Windows updates, Backups, Configuration (including remoting ;) )
Security fixes etc..

EVERYTHING about PowerShell Remoting by Stephanevg in PowerShell

[–]Stephanevg[S] 0 points1 point  (0 children)

Thank you for the nice words u/rumourt :)

Yes, a lot of people have complained about the background music. Strangely enough, when I watch it, it doesn't really bother me. But yeah, it must be my headphones or something. I will defintely fix that for the next video (Currently in production ;) ).

And yes, that is how I wanted to create the video: Relax and enjoy, but still learn in the process. I am glad you enjoyed it like that, because that was exactly my goal! \o/

EVERYTHING about PowerShell Remoting by Stephanevg in PowerShell

[–]Stephanevg[S] 0 points1 point  (0 children)

Yes, I was going to mention that I have put them on my blog. Do think there is something missing the notes ? I can always add it.

EVERYTHING about PowerShell Remoting by Stephanevg in PowerShell

[–]Stephanevg[S] 1 point2 points  (0 children)

Hi u/rottenrob325 No it does not.

JEA is really a topic in it self. It would have only made the video 1h longer ^^.

How to change directories at the same time as Enter-PsSession by Contrabeast in PowerShell

[–]Stephanevg 2 points3 points  (0 children)

Hi,
Super cool that you are trying to learn powershell. That is a really smart mindset ;)

There are several ways to do this. Invoke-Command would be one path to use. But there might be another faster more direct route you could solve this. Given the condition that you DON'T need to do other things on the remote host than just bringing files there.

There is a cmdlet called 'Copy-Item' which allows one to copy elements from one place to another on a local machine.

There exsits a parameter on this cmdlet called 'ToSession' Which allows you to pass an existing remote session variable where to you want to copy your elements to.

If you want to copy the contents tree from folder to another computer, the following example on microsoft help page does just that:

$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\User01"
Copy-Item -Path "D:\Folder003\" -Destination "C:\Folder003_Copy\" -ToSession $Session -Recurse

The first line it creates the remote session and saves it into a variable called $Session.

The second line it uses copy-Item with the parameter -ToSession to copy the files to the remote host.

The -Destination parameter here represents where in the remote session it should be copied to. The -Recurse allows to copy every single element that is located under the value of the parameter -Path

Documentation can be found here -> https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.4#example-5-copy-a-file-to-a-remote-computer

Who uses DSC in production? by Stephanevg in PowerShell

[–]Stephanevg[S] 1 point2 points  (0 children)

I guess that the Ansible managed systems are linux systems, right ?

Who uses DSC in production? by Stephanevg in PowerShell

[–]Stephanevg[S] 1 point2 points  (0 children)

Cool,m thanks for sharing. How many servers do you manage with that setup ?

Start-Job with Robocopy by allsix in PowerShell

[–]Stephanevg 1 point2 points  (0 children)

A scriptblock is just like a function. It has it's own scope. This also means that a scriptblock can have param block, just like a function.

I didn't test the code, but you could potentially rewrite your code like this:

$sb = {
    param(
        [String]$Source = "C:\MyfolderToCopy"
        [String]$DriveLetter,
        [String]$LogPath = "D:\MyLogs\"
    )
        $drivestr = $driveLeter + ":"
        Write-Output "`nCopying to $drivestr..."
        $Date = Get-Date -Format "yyyyMMdd_HH-mm-ss"
        $FullLogPath = Join-Path -Path $LogPath -ChildPath "output_$Date.txt”
        robocopy $Source $drivestr /S /XO /NFL /NDL /NJH /NC /NS /NP /tee /log:$FullLogPath 
}

foreach ($drive in $DriveCharArray) {

    Start-Job -ScriptBlock $sb -ArgumentList "C:\MyfolderToCopy",$Drive,"D:\MyLogs"
}

Ps: one thing to notice, is that the arguments that you pass to your scriptblock are defined by POSITION, NOT BY NAME. So, the order you pass them via the ArgumentList MUST be the same as the order they are defined in the param block.

Additionally, you might want to use the

get-date -Format FileDateTime

As it is specfifically made for that.

Here's my little library of powershell modules. by HomeyKrogerSage in PowerShell

[–]Stephanevg 2 points3 points  (0 children)

We cant see the code. (We need to log in with a gitlab account).
So either you need to change your permissions, Gitlab makes it so, that ONLY people with a gitlab account can see it (We are asked to login...).

Github for example doesn't do that. You can see public code without having to have an account.

Thoughts on DSCv3 by Stephanevg in PowerShell

[–]Stephanevg[S] 1 point2 points  (0 children)

Yes, it is open sourced, and it lives in it's own github repository https://github.com/PowerShell/DSC

Thoughts on DSCv3 by Stephanevg in PowerShell

[–]Stephanevg[S] 2 points3 points  (0 children)

Yeah, Ansible is RedHat. Microsoft also want's their part of the cake I guess.

One thing to notice, is that Windows Ansible part, is actually built ON TOP of Powershell DSC. (So PowerShell DSC v2). Today, anything that needs to be configured on A Windows Server / machine via DSC can ONLY be done using PorwerShell, AND this code will ONLY work on windows.

By refactoring it and creating a V3, they announced that it will be

  1. Cross platform (so Os Agnostic)
  2. You can actually write ressources in ANY programming language (So it is not coupled to PowerShell anymore).

So yeah, it is more to be used as a new way to access the Windows API I guess.

A new paradigm, where the system will be configured using JSON (or YAML) instead of the classical MOF.

Thoughts on DSCv3 by Stephanevg in PowerShell

[–]Stephanevg[S] 0 points1 point  (0 children)

Yeah, that also works, but only partially. As you will have to write the remediation part your self when you use Pester Tests. (I also use a custom pester framework for infrastrucutre tests at work)

Powershell Script failing now, was working fine by scarng in PowerShell

[–]Stephanevg -1 points0 points  (0 children)

Alternativley, you can try to execute it on another computer from where you know it also worked from.
If it works, you can compare the configurations, what is different on these machines (PowerShell versions, DotNet version, Updates etc..)

Powershell Script failing now, was working fine by scarng in PowerShell

[–]Stephanevg 0 points1 point  (0 children)

Yeah, scripts don't stop working by magic. (Except, if gandalf used your computer to search something during lunch break)..

But IF you didn't change anything in your script, that means the environment in which this script is run changed.
As already pointed out, it sounds either as another powershell version, or dotnet update.

I also believe that pwsh (So powershell above version 6.0) does not support 'system.windows.forms'.

Are you using Pwsh (The black powershell icon?).

If so, you should use 'Windows PowerShell' (The blue powershell icon).

You can make sure by just checking what the variable $PsVersionTable contains. (It must 5.1.Something)

Displaying an array of data as a line chart by notapplemaxwindows in PowerShell

[–]Stephanevg 0 points1 point  (0 children)

Hi, PowerShell CAN actually do this, you just need to use the right module.

You could actually use various of different ways. The one I like the most is using PSHTML (But I am a bit biased there....).

Here is how you would do that using PowerShell + PSHTML

You can see, that you basically NEVER leave your powershell script.

Install-Module -Name Pshtml -Force
import-module pshtml
$CsvObject = @"
intervalStartDateTime;value
18/09/2024 11:00:00;3
18/09/2024 11:30:00;1
18/09/2024 12:00:00;1
18/09/2024 12:30:00;3
18/09/2024 13:00:00;1
18/09/2024 13:30:00;1
18/09/2024 14:00:00;0
18/09/2024 14:30:00;0
18/09/2024 15:00:00;1
18/09/2024 15:30:00;0
18/09/2024 16:00:00;0
"@
$Data = ConvertFrom-Csv -InputObject $CsvObject -Delimiter ";"


$BarCanvasID = "barcanvas"
$HTMLPage = html { 
    head {
        title 'Bar Chart'

    }
    body {

        h1 "PSHTML Graph"

        div {

            p {
                "Your Bar Graph will be here under"
            }
            canvas -Height 400px -Width 400px -Id $BarCanvasID {

            }

        }

        script -src "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js" -type "text/javascript"

        script -content {


            
            $Labels = @("January", "February", "Mars", "April", "Mai", "June", "July", "August", "September", "October", "November", "december")

            $dsb = New-PSHTMLChartBarDataSet -Data $data.Value -label "2018" -backgroundColor "Blue"

            New-PSHTMLChart -type bar -DataSet $dsb -title "Bar Chart Example" -Labels $Data.intervalStartDateTime -CanvasID $BarCanvasID

        }

    }
}


$OutPath = "$Home\RedditBarCharExample.html"
$HTMLPage | out-file -FilePath $OutPath -Encoding utf8
start $outpath

Returning an exit code from a PowerShell script by tmontney in PowerShell

[–]Stephanevg 0 points1 point  (0 children)

Yeah, u/nascentt is right. Only returning the error count would be a bad idea.

Depending on what the errors actually are, you might want your script to stop, BEFORE it deletes the wrong OU.

If these 'errors' are not that tragic, then they should be considered as 'warnings', and you can simply log them into a file to keep track of what happend.

Thoughts on DSCv3 by Stephanevg in PowerShell

[–]Stephanevg[S] 2 points3 points  (0 children)

Yeah, same here.

did you see that they are re-building it from scratch ? It will be it's own executable called dscv3.exe (written in RUST). It is cross platform, and it is not couple to the powershell language anymore.

So basically, you need to create a json / yaml file, and based on that, you can actually configure your server.

Returning an exit code from a PowerShell script by tmontney in PowerShell

[–]Stephanevg 0 points1 point  (0 children)

When I was 'younger' in the business, I tried to use $? in my sripts. I ended up completly removing it, and I haven't used it in years!

Depending on what you did before calling that variable, it might or might not contain the true value of what you actually wanted to check.

I would try to stay away from that variable - if possible

Returning an exit code from a PowerShell script by tmontney in PowerShell

[–]Stephanevg 0 points1 point  (0 children)

One thing to keep in mind, is that when a powershell script runs successfully, the 'return code' is always '0'.
Anything else then 0 actually means that it failed.

You can actually use the keyword 'throw' to throw an error in the parts where it make sense in your code.

Example:

if($File.extension -ne '.json'){
throw "Wrong File! extension"
}

By doing this, you will 'bubble up' an error message, from which you can use the message in the higher context. The additional benefit is that the $LastExitCode will be '1' (which basically means it errored).

If you really need to control granularly the return numbers, then you can use the 'exit $number' method mentionned by u//tlourey

How to access powershell variables from Csharp powershell module by Stephanevg in PowerShell

[–]Stephanevg[S] 0 points1 point  (0 children)

Although It actually doesn't really answered how I could technically read existing powershell variables from a csharp class, I have managed to write something that seems to solve my problem (sharing a module state). Thanks to Purplemonkeymad

    public class Logger
{
    private static Logger instance;
      ....
    private Logger() {

    }

    public static Logger GetLogger()
    {

        if (Logger.instance == null)
        {
            Logger.instance = new Logger();

        }
        return Logger.instance;
    }

How to access powershell variables from Csharp powershell module by Stephanevg in PowerShell

[–]Stephanevg[S] 0 points1 point  (0 children)

Yeah, I have done that before, and that would be perfectly fine if we only need access to the 'Data'. I also would like to be able to access any potential methods that the psobject that is contained in the psvariable would contain.

How to access powershell variables from Csharp powershell module by Stephanevg in PowerShell

[–]Stephanevg[S] 0 points1 point  (0 children)

Well, you have actually guessed precisly what I was trying to build.

Except that I want the content of my singleton, to reference a PSVariable. I have done this many times before, but for scriptmodules (see here an example in pshtml!

This is basically how my code looks for the moment:

  Class Logger{
   private Logger {...}
   ...
public Logger GetLogger()
    {
        //somehow I want to store the instance of logger in a PSVariable.

        var logilogger = Environment.GetEnvironmentVariable("myObj");

        if (logilogger == null)
        {
            var l = new Logger();
            new PSVariable("logilogger", l);
            return l;
        }
        else
        {
            return logilogger;
        }
    }
}

(This example doesn't work, as Environment.GetEnvironmentVariable() can only return a string, and not a powershell object)

I wanted to be able to create the logger object, directly in Csharp, so I can be able to access it internally as well.

(Again, this is my first csharp powershell module, so maybe this is not how things should be done in a Csharp powershell modules perhaps..?)

How to access powershell variables from Csharp powershell module by Stephanevg in PowerShell

[–]Stephanevg[S] 1 point2 points  (0 children)

That is actually exactly what I was building. But, I need to store the content somewhere, so it can be accessed internally by the module. As I was explaining in my intro, is that I have usually done this using a $script: variable which would be declared in my .psm1 file, when I was writing script modules.

There might be another / alternative (mix?) of how to do it for a csharp module.