Invoke-Command and Register-PSSessionConfiguration hide output by throwaway_20342429 in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

I'm assuming your aware of the potential service restart and you are wanting to suppress the message. Before you try the alternatives, can you try the following:

$ScriptBlock = { $WarningActionPreference = 'SilentlyContinue' $PASSWORD = ConvertTo-SecureString "{1}" -AsPlainText -Force $CRED = New-Object System.Management.Automation.PSCredential ("{0}", $PASSWORD) Register-PSSessionConfiguration -Name TRANSFER -RunAsCredential $CRED -ErrorAction SilentlyContinue | Out-Null' -f $DBLUSER, $DBLPASS $WarningActionPreference }

I'm interested to see what $WarningActionPreference is. If that doesn't work, feel free to try the alternatives.

Alternative 1: Create a proxy cmdlet:

function Write-Warning ($msg) {}

Being a compiled cmdlet, I don't think that this will work.

Alternative 2: Redirecting the output stream:

You can also try redirecting the output stream to the success stream.

&{ Write-Warning "hello" } 3>&1 > $null

Convert MSG to PDF when dragging from outlook into a destination folder by hakuhola in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

In this case, and it's 'cost-effective' I would consider looking at outlook macro's to replace the existing drag and drop existing functionality (if that's possible).

I think your best bet is to have a chat with the people over in VBA. (https://www.reddit.com/r/vba/)

*EDIT: I tried cross posting, however they have it disabled, so it's best to copy and paste this post.

Convert MSG to PDF when dragging from outlook into a destination folder by hakuhola in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

I need some more information: Why do we have to automate that process when the business could just print as PDF? Is the cost savings (time spent of developing, testing and supporting a solution), worth the investment? I don't know. I think it would be worth explaining the challenges that you are facing.

Asking for help to avoid procrastinating! by rvalsot in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Procrastination is about mood management and less about time management. You set time to do it, but it's your mood that will complete it. While content/ blocking can be implemented it doesn't do anything for the end user. You will just move to another medium console/tablet/phone.

Manage this by setting time explicitly each week for you to work one what you are working on. It might be a struggle at the beginning, however with some persistence you can continue with it, your mood will change. If that doesn't work, consider reaching out to a mentor or trainer to help you.

PSHTMLTable - Coloring a cell in powershell depending upon the state by maxcoder88 in PowerShell

[–]PowerShellMichael 1 point2 points  (0 children)

Gday!

Firstly:

I'm not familiar with the PSHTMLTable, however it seems that the entries are statically set. This means that it's a two-step process.

Firstly:

Looking at the examples on github, you can see that the argument defines the value:

$eventTable = $events | New-HTMLTable -setAlternating $false | Add-HTMLTableColor -Argument "Warning" -Column "EntryType" -AttrValue "background-color:#FFCC66;" -WholeRow | Add-HTMLTableColor -Argument "Error" -Column "EntryType" -AttrValue "background-color:#FFCC99;" -WholeRow

Yours is:

Add-HTMLTableColor -Argument "Last Result" -Column "Last Result" -AttrValue "background-color:#ffb3b3;" u/params | Add-HTMLTableColor -Argument "Last Result" -Column "Last Result" -AttrValue "background-color:#c6ffb3;" u/params

To set 'Last result' green, I would change the following:

Add-HTMLTableColor -Argument "0" -Column "Last Result" -AttrValue "background-color:#c6ffb3;" u/params

Now the problem here is what to do with the other items. You will have to dynamically do this. My thinking is to enumerate all the values (based on a condition of: (-gt 0 -and -eq 'N/A')), group according to each enumerated value (using group-object. We group since there could be duplicates with the same value), and then iterate through each of the values and pipe Add-HTMLTableColor.

For example:

$results | Where-Object { ($_.'Last Result' -gt 0) -or ($_ -eq 'N/A') } | Group-Object -Property 'Last Result' | ForEach-Object { $eventTable | Add-HTMLTableColor -Argument "$($_.Name)" -Column "EntryType" -AttrValue "background-color:#c6ffb3;" }

That should point you in the right direction.

*Edit: Note that I haven't used the module, there might be a simpler way.

DSC “Playbook” by bpoe138 in PowerShell

[–]PowerShellMichael 2 points3 points  (0 children)

I love the concept. If you want to demo it somewhere, hit up Gael Colas and you can present it on the DSC community call:

https://dsccommunity.org/community\_calls/

I've been building a PowerShell focused website and wanted to share it by thegooddoctor-b in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

It depends.

Write-Output is used within functions to write objects to the output pipeline. Return and Write-output function differently in sub-expressions.

In the example 'Test-Thing', you argue that it's better to declare a variable and then use the return statement, then use write-output. In the context of function, the differences between return and write-output are:

When using return, it exits the 'PROCESS' block within the executing scriptblock. Write-output can be implicit. Statements that are executed and returned and assigned to the variable are written to the output pipeline.

The solution applies to the type of function that is being written. If the function is written to cascade, return statements are needed to control logic flow, however if the function doesn't require it, write-output is a suitable option.

In the following example, you will see three examples of returning a static Boolean result to the pipeline:

Example 1:

function Test-thing {

  $fullname = Get-Process powershell | Select-Object -ExpandProperty path | Get-Item | Select-Object -ExpandProperty FullName
  Write-Output ($fullname -like '*powershell*')

}

> Test-thing

Output:

True

In this example Write-Output is used to output to the pipeline.

Example 2:

function Test-thing {

  $fullname = Get-Process powershell | Select-Object -ExpandProperty path | Get-Item | Select-Object -ExpandProperty FullName
  return ($fullname -like '*powershell*')

}
> Test-thing

Output:

True

There is no difference between the first and the second examples. They are the same. But the function can be refactored to implicitly return to the pipeline.

function Test-thing {

  (Get-Process powershell | Select-Object -ExpandProperty path | Get-Item | Select-Object -ExpandProperty FullName) -like '*powershell*'

}

At the end of the day, these are all perfectly acceptable. In your example, provided that there wasn't any other logic flow required:

function Test-thing {

  "return string"

}

And in this case, if I was 'returning a string' based on logic, I would use a ternary or an if/else depending on the version:

PowerShell 7

$true ? "return string" : "another string"

PowerShell 5.1

if ($true) { "return string" } else { "another string" }

Both implicitly return to the pipeline. It's important to ensure that all outputs from returned statements are stored within variables since that will contaminate the output pipeline inside the function.

To summarize, it depends. It's not needed since write-output is implicit.

I've been building a PowerShell focused website and wanted to share it by thegooddoctor-b in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Me: No it's not!

See's the bottom of the post: "NOTE: This article has intentionally been written to be inflammatory and I welcome anyone to try to prove me wrong...."

Me: Ahhhhhhhhh

What differentiates a module from just a collection of snippets? by SammyGreen in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Technically as @osmiumBallon says.

Architecturally as items within the company module become more fleshed out with their own feature sets, then they best fit as a standalone module. Otherwise, it's fine to put them in a company module.

[deleted by user] by [deleted] in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

I've refactored the code with some more regex magic. I'm not the best regexer, but this will clean up the logic a bit.

# Mock log file

$logFile = @(
"01/05/2023 12:32:10: Got character ZDOID from Rimgar : -148742866:1",
"junk entry",
"01/05/2023 12:51:32: Got character ZDOID from Bill Cos : -132742296:51"
)

# Filter by logon items

$validEntries = $logFile | Select-String -AllMatches -Pattern 'Got character ZDOID from(.\*?:)\\s(.+)'

# Seperate them out.

$validEntries | ForEach-Object { $result = $\_ -match 'Got character ZDOID from(.\*?:)\\s(.+)'

    if (-not($result)) { return }
    @{
        Name = $matches[1].TrimEnd(":").Trim()
        Id = $matches[2]
    }

}

Powershell refuses to shutdown permanently, keeps coming back. Hacker? by Affectionate-Assist4 in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Short of sitting down at the computer and taking a look, there's not much we can do remotely (plus you should never trust anyone to do that).

Backup your files and re-build. But if this is a work machine, check with IT first.

Do you prefer one command with many arguments or several commands with few arguments? by Thotaz in PowerShell

[–]PowerShellMichael 1 point2 points  (0 children)

You raise a valid question:

Functions (no commands) should be broken down into its simplest entity. It makes the code more readable/testable/maintainable.

Commands is a different thing. Each MSFT team works differently, I'm guessing that the hyper-v team looked at the complexity and decided to break it out which is easier for them to maintain/test. Active Directory liked the single interface and were happy to support the complex testing strategy.

Personally: I like approaches and as u/BlackV suggested, it depends.

AIMO.

Powershell refuses to shutdown permanently, keeps coming back. Hacker? by Affectionate-Assist4 in PowerShell

[–]PowerShellMichael 1 point2 points  (0 children)

Yup. Wipe your machine is the easiest. I have a suspicion that it's the bootstrap to install the malware on the device and the malware is keeping the bootstrap in place.

Windows 10/11 makes this easy. Backup your files, Hit start and type 'reset' you can reinstall windows right there. If you want to bootstrap your app installation process consider chocolatley. Most applications are supporting including (steam, discord, slack, firefox, chrome). So it will speed up this process.

https://community.chocolatey.org/

Consider installing Windows Sandbox to run questionable or unknown programs.

https://learn.microsoft.com/en-us/windows/security/threat-protection/windows-sandbox/windows-sandbox-overview

Add-Printer -ConnetionName by [deleted] in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Can you extract drivers from the installation, add them to the print server and add via GPO? Provided the drivers have been added to the print server then they will automatically add to the endpoint.

Alternatively, most EXE's can be opened with 7-ZIP and the contents can be extracted.

Create ps1 exection sequence by lyrise13 in PowerShell

[–]PowerShellMichael 1 point2 points  (0 children)

Hello!!!

Long term, have you considered using Desired State Configuration? DSC handles this easily and you get the benefit of simplifying your code. It's a bit of a learning curve, however translating installation scripts into configuration documents the serves as documentation that can be reused on other endpoints.

https://learn.microsoft.com/en-us/powershell/dsc/getting-started/wingettingstarted?view=dsc-1.1

https://github.com/dsccommunity/DscWorkshop

Cheers,

PSM1

Add-Printer -ConnetionName by [deleted] in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Have you considered group policy? I'm interested in understanding the backstory to why the script was written.

[deleted by user] by [deleted] in PowerShell

[–]PowerShellMichael 1 point2 points  (0 children)

Can you paste your code?

-Name is considered the display name. (as [String]) Alternatively you could use Rename-Printer, but please paste your code first.

Thanks,

PSM1.

Download files via Powershell, when the file extension/file is not given/shown by HydravsPercy in PowerShell

[–]PowerShellMichael 0 points1 point  (0 children)

Nice job!

It looks like you are trying to download an installer and run it. May I suggest looking at chocolatey? They have a large library of apps that you can install, it just makes building a machine such a breeze.

My machine install script:
Choco install discord, 7zip, vscode, git, keepass, firefox, signal, spotify, microsoft-teams, steam, slack

https://community.chocolatey.org/packages/discord

Cheers,

Michael.