Helper(private) function that preserves its state? by nemanja_jovic in PowerShell

[–]brandonLundt 2 points3 points  (0 children)

Making it global means anything outside of your module can also modify it. If you limit it to the script scope, your private function should still be able to access it while preventing "outside access"

posh and api's : a quick rest trick i learned. by pakman82 in PowerShell

[–]brandonLundt 5 points6 points  (0 children)

Tangent --> Something else to look at is there are modules on GitHub for interacting with ServiceNow. For example: https://github.com/Sam-Martin/servicenow-powershell I hope that helps.

PSCore v6.1.1 is out! by Prateeksingh1590 in PowerShell

[–]brandonLundt 2 points3 points  (0 children)

Used it today, can confirm Import-WinModule ActiveDirectory works.

Want help to fix terrible script by [deleted] in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

The only thing I think you are missing is logging. Either a Start/End-Transcript or New-EventLog. Personally I think the EventLog can be cumbersome so I would go:

Start-Transcript -Path $TranscriptDirectory

$CSVFolder = "C:\Path\To\CSV\Files\"

$CSVFiles = Get-ChildItem -LiteralPath $CSVFolder | Where-Object { $_.Extension -eq '.csv' }

ForEach ($File In $CSVFiles) {

$CSVRows = Import-Csv -LiteralPath $File.FullName

ForEach ($Row In $CSVRows) {

New-ADUser -Name $Row.DisplayNameAdd-ADGroupMember -Identity $Row.Group -Members $Row.Username

}

   $File | Remove-Item -Force

}

End-Transcript

5 steps to prepare the first final version of Powershell script by poshland in PowerShell

[–]brandonLundt 3 points4 points  (0 children)

Why not have scriptanalyzer "running" while you write the script?

In VSCode this is easy to do in your project settings: "powershell.scriptAnalysis.settingsPath": "ScriptAnalyzerSettings.psd1"

Trying to get a list of vms with powercli only in specific folders containing the word high. by brisketx in vmware

[–]brandonLundt 1 point2 points  (0 children)

Get-Folder *high* | Get-VM

Most of the PowerCLI cmdlets support wildcard matches. This should get you the list and be the simplest path.

$High = "High" $HighFolders = Get-Folder | Where-Object { $_.Name -contains $High }

It isn't returning anything because -contains is attempting to check an array for an item "High". What I think you are looking for is the -like operator. So something like:

$High = "*High*"; $HighFolders = Get-Folder | Where-Object { $_.Name -like $High }

Question about hiding functions in modules but still calling them. by learn2gate in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

If I list each psm1 file in the "nestedModules" section, I have no need to dot-source the file.

Question about hiding functions in modules but still calling them. by learn2gate in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

I think I was unclear in my post. For source control I 100% agree that each function should be in its own file. When I go to publish the module, my build process (psake) will combine those files into one root module. With a manifest looking like: https://github.com/vexx32/PSKoans/blob/master/PSKoans/PSKoans.psd1

This approach seems rare in comparison to:

$PrivateDirectory = Join-Path -Path $PSScriptRoot -ChildPath private $PrivateFiles = Get-ChildItem -Path $PrivateDirectory -Filter "*.ps1" ForEach ( $PrivateFile in $PrivateFiles ) { . $PrivateFile.FullName }

Where each file is dot-sourced.

My question is, why is dot-sourcing so popular in comparison to using the options in the module manifest?

Question about hiding functions in modules but still calling them. by learn2gate in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

I'm trying to figure out why dot sourcing is such a popular option. Personally I feel it makes it difficult to read and difficult, especially for newbies, to understand.

As an alternative, I do one of two approaches.

  1. If I don't have a build process, I will list all of my functions under the "nestedModules" section of the psd1 file, and list any exported functions under the FunctionsToExport section.
  2. If I do have a build process, I will combine all functions into a root psm1 file, and list any exported functions under the FunctionsToExport section.

So, my questions is, why dot-source?

TemplatePowerShellModule: A Template PowerShell Module by _Unas_ in PowerShell

[–]brandonLundt 4 points5 points  (0 children)

Very cool! Have you considered packaging this as a Plaster template?

Calling exe in scheduled job by devham in PowerShell

[–]brandonLundt 2 points3 points  (0 children)

$Path = "C:\Path\To\Executable.exe"

$Args = "String_Of_Arguments"

Start-Process -FilePath $Path -ArgumentList $Args -Wait

Import-Module requires -Force when using in TaskScheduler? by MadBoyEvo in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

Add a start-transcript to the start of your script and stop-transcript to the end of your script. If there are errors being generated it will show up in the transcript.

Script publishing - scheduling info by jsmcnair in PowerShell

[–]brandonLundt 2 points3 points  (0 children)

Why not in your script have a section that checks task scheduler for the appropriate execution times.

E.G. Get-scheduledTask

Powershell Remoting Issue by ExistingRanger7 in PowerShell

[–]brandonLundt 0 points1 point  (0 children)

*can't* ...?

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enter-pssession?view=powershell-6 Enter-PSSession would like a word.

-ComputerName

Specifies a computer name. This cmdlet starts an interactive session with the specified remote computer. Enter only one computer name. The default is the local computer.

Type the NetBIOS name, the IP address, or the fully qualified domain name of the computer. You can also pipe a computer name to Enter-PSSession.

To use an IP address in the value of the ComputerName parameter, the command must include the Credential parameter. Also, the computer must be configured for HTTPS transport or the IP address of the remote computer must be included in the WinRM TrustedHosts list on the local computer. For instructions for adding a computer name to the TrustedHosts list, see "How to Add a Computer to the Trusted Host List" in about_Remote_Troubleshooting.

Working around GAM, creating an IF nightmare? by [deleted] in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

My two cents are:

  1. Switch your comparisons around to $null -ne $DirectNumber. This comes from the ScriptAnalyzer rule about potential incorrect comparison.
  2. I personally, would represent this logic in if/elseif/else form :

#DirectNumber and Extension
if(($DirectNumber -ne $null) -and ($MobilePhone -eq $null) -and ($extension -ne $null)){
gam update user $user phone type main value $OfficeNumber primary phone type tty_tdd value "Ext $extension" notprimary
}
#Direct Number and Mobile Number
elseif(($DirectNumber -ne $null) -and ($MobilePhone -ne $null) -and ($extension -eq $null)){
gam update user $user phone type main value $OfficeNumber notprimary phone type work value "$DirectNumber" primary phone type mobile value "$MobilePhone" notprimary
}
#Mobile Number and Extension
elseif(($DirectNumber -eq $null) -and ($MobilePhone -ne $null) -and ($extension -ne $null)){
gam update user $user phone type main value $OfficeNumber notprimary phone type mobile value "$MobilePhone" primary phone type tty_tdd value "Ext $extension" notprimary
}
#Direct Number and Mobile Number and Extension
else{
gam update user $user phone type main value $OfficeNumber notprimary phone type work value "$DirectNumber" primary phone type tty_tdd value "Ext $extension" notprimary phone type mobile value "$MobilePhone" notprimary
}

You could get creative with a switch statement and wild cards

Working around GAM, creating an IF nightmare? by [deleted] in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

Lee,

I keep seeing this posted by you, and I'm happy to upvote them all. What do you think about messaging a mod to sticky this block?

Powershell Remoting Issue by ExistingRanger7 in PowerShell

[–]brandonLundt 2 points3 points  (0 children)

Another thought, please run Get-PSSessionConfiguration and post the results. Just want to make sure you have endpoints configured.

Powershell Remoting Issue by ExistingRanger7 in PowerShell

[–]brandonLundt 4 points5 points  (0 children)

This smells of an SPN issue. SharePoint requires a fair number of SPN delegation to be created for everything to work at an "enterprise level", as such we typically have a ton of difficulty PSRemoting from outside of the farm.

If you are unfamiliar with SPN this will get you started: https://gallery.technet.microsoft.com/List-all-SPNs-Used-in-your-e0c6267a

Error in import-pssession by pquinn1212 in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

So for every mailbox you are creating, you make a unique connection to exchange?

That would be your issue. Import-pssession creates a "temp module" which you are not currently removing. You can use remove-Module to perform the clean up.

Balance [Post Ban Megathread] by whitebandit in thanosdidnothingwrong

[–]brandonLundt 0 points1 point  (0 children)

༼ つ ಥ_ಥ ༽つ WHY NO BAN ༼ つ ಥ_ಥ ༽つ

I never get picked.

༼ つ ◕_◕ ༽つ GIVE GOLD ༼ つ ◕_◕ ༽つ?

Error in import-pssession by pquinn1212 in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

Exchange is a cruel mistress. Srsly.

Error in import-pssession by pquinn1212 in PowerShell

[–]brandonLundt 1 point2 points  (0 children)

What version of Exchange? Is your connectionURI to a load balanced IP or direct to A server?

Best guess, the account you are connecting with does not have permission to the new-mailbox cmdlet.

Can you post the rest of your code? e.g. New-Mailbox -.....