I copied files from an Ubuntu VM to laptop's WSL and suddenly all the files were garnished by Zone.Identifier. After some nudging, ChatGTP provided the following:
find . -type f -name '*:Zone.Identifier' -exec rm '{}' \;
I copied files from an Ubuntu VM to laptop's WSL and suddenly all the files were garnished by Zone.Identifier. After some nudging, ChatGTP provided the following:
find . -type f -name '*:Zone.Identifier' -exec rm '{}' \;
I get a few comments on my blogs and today I received this comment on this post, PowerShell: $Myinvocation.ScriptLineNumber behaves incorrectly with Class Methods:
Here is a function that will include the same number as the Write-Host is on:
To see the line number of Write-Host consider the following screenshot:
When the above code is run (in this case Visual Studio Code using the PowerShell extension) the following is displayed showing the Write-Host is on line 5:
Using the runas command from a Windows console windows was demonstrated in the post, Access SQL Server Management Studio using a different Active Directory Credential. The concept was that a user was logged in to Windows and an application (SQL Server Management Studio e.g. ssms.exe) needed to be run as a different user (hence the use of runas).
The much vaunted Windows Sysinternals was acquired by Microsoft in 2006 and among the useful utilities produced by Sysinternals is ShellRunas. As the previous documentation link shows:
ShellRunas can be downloaded and zipped from Download ShellRunas. The download, ShellRunas.zip contains an executable ShellRunas.exe. To install ShellRunas as as a Windows shell extension, run the following from a Windows console terminal in the folder containing ShellRunas.exe:
Invoking ShellRunas as shown above displays a dialog indicating the shell extension has been added;
From Windows Explorer right click on the application (such as ssms.exe) which displays a context menu such as the following:
The bottom option of the context menu is "Show more options." Select this menu item which displays the following context, sub menu:
From the above context menu select "Run as different user" which display the following dialog courtesy of Sysinternals ShellRunas:The above dialog can be used run an application as a user that differs from the currently logged in Windows user.
There is no way to change the "User name" in SQL Server Management Studio's "Connect to Server" dialog when using Windows Authentication. This post demonstrates how to run SQL Server Management Studio (SSMS) and authenticate a connection using a different user than the current active Windows user while authenticating with Windows Authentication. For example, a user logs in with the normal account but needs to authenticate with an administrative account to access SQL Server.
An example of SSMS's "Connect to Server" dialog authenticating a connection with Windows Authentication is as follows:
The "User name" dropdown above is grayed out as it cannot be changed because Authentication is set to Windows Authentication. The Windows runas command-line utility (see Runas) can be used to run SSMS or any application as a different user than the user currently logged into a host. For this example, considering desiring to login as:
To run SQL Server Management Studio as user domainAbc\userEfg, specify the following from a console prompt:
runas /noprofile /netonly /user:domainAbc\userEfg "C:\Program Files (x86)\Microsoft SQL Server Management Studio 19\Common7\IDE\ssms.exe"
Once the above command is run, the user will be prompted to enter a password for account domainAbc\userEfg:
Enter the password for domainAbc\userEfg:
There is no way to specify the password as a runas command-line option so the password must be manually entered.
The following is all the text displayed when running SSMS as a different user via the runas command:
Microsoft provides an excellent example of how to create a report of Azure App Registrations with expiring secrets and certificates (see: Export app registrations with expiring secrets and certificates). This script uses Microsoft's Graph PowerShell SDK module which is installed as follows:
In Microsoft's sample the following PowerShell snippet is included in two separate locations in the code:
On a Windows host where Docker is configured to use Windows (versus Linux) contains the following error message, "Windows Containers are not supported on your version of windows", can be encountered. There are multiple reasons for this error message but one of the most common is that it is only possible to run Windows containers on Windows Pro or Windows Enterprise edition. Docker cannot run Windows containers on Windows Home or Windows Education edition.
Docker's setup instructions for Windows can be found at Install Docker Desktop on Windows. A the top of this web page, under System Requirements is the following (as of August 23, 2023):
There is nothing in the above documentation stating that Windows Home and Education only support Linux containers. There is nothing in the above documentation stating that in order to supports both Linux and Windows containers Windows Pro or Windows Enterprise is required.
Note: I have submitted a Pull Request to Docker's documentation repo (Docker docs) requesting the text be changed to include the sentences "Home and Education editions support only Linux containers. Pro and Enterprise editions support Windows containers and Linux containers." alongside the Windows 11 and Windows 10 system requirements.
Dockers Windows install documentation (Install Docker Desktop on Windows) does include a note well below the "System requirements" tab that presents which editions of Windows are required in order to run Windows containers:
I was handed a laptop for a project that was Windows 10. Windows Terminal was not installed. On Windows 11 22H2, Windows Terminal is the default "command line experience" (see Windows Terminal is now the Default in Windows 11). Microsoft recommends installing Window Terminal using the Windows Store (see Windows Terminal). The laptop I was given was a corporate laptop and Windows Store was unavailable.
It is possible to download and install Windows Terminal from Windows Terminal Releases. Below are assets for Windows Terminal v1.17.11461.0:
In PowerShell the standard way to create a temporary filename is to invoke the System.IO namespace's Path class's GetTempFileName method. To be clear, only a filename is retrieved and no actual file is created. An example of GetTempFileName being invoked by PowerShell is as follows:
The results of the code above will vary because the filename is randomly generated and the method uses a user's environment variable, $env:TEMP. An example of the value assigned to $tempFilenameFromCsharp when the code snippet above being invoked is as follows:
C:\Users\jann\AppData\Local\Temp\tmpCF88.tmp
For those that need a reminder, the WhatIf command-line option is defined as follows (see: WhatIf Switch):
My attempt to use New-TemporaryFile to create filename without creating a file was as follows:
The output from invoking the above command is follows:
Not every idea we try works.
One of the most optimal ways to upload and download blobs to/from Azure storage containers is the azcopy utility (see azcopy). This utility can be used from the command-line or can be included in a PowerShell script such as the following:
The output from the above command is as follows:
Before installing the latest version of Pester, uninstall the legacy version of Pester (Pester 3.x) which is installed with most modern versions of Windows (see PowerShell: Uninstalling Pester 3.0). On a machine with PowerShell 5.0 or later install, the latest version of Pester can be installed as follows without running as administrator:
Once install verify that version of Pester is the most recent:
(Get-Module -ListAvailable Pester).Version
An example of installing Pester is as follows (not that a user is prompted to accept the modules being installed):
E:\Users\Jann\PowerShellRepos> Install-Module -Name Pester
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository
cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A
Microsoft by default installs an obsolete version of Pester on Windows (Pester 3.x). For example on one of my machines (Windows 11) the version of Pester installed is 3.4.0 (2016 Pester). In fact, Pester 3.4.0 is installed for x64 and x86 (32-bit) versions of PowerShell. To uninstall Pester 3.4.0 means that the x64 and x86 installs have to be uninstalled.
The following PowerShell displays available versions of Pester installed:
The default version of Pester installed on my Windows 11 host is as follows:
Placing the following at the top of a PowerShell script requires that said script to run as administrator:
#Requires -RunAsAdministrator
Two years ago I wrote a post, PowerShell: Inadvertently Returning Multiple Values from a Function and low and behold I found a found a common C# data type that is a common culprit of this issue, StringBuilder. I have coded C# for twenty-tree years and I did not realize the each Append* method of StringBuilder returns a reference to the StringBuilder.
To demonstrate consider this C# snippet:
In the documentation for the AppendLine method, AppendLine(String), the return value of AppendLine and each Append* method of StringBuilder is defined as follows:
A clearer way to write the above code in C# would be acknowledge the return value and to ignore it:
The following code shows PowerShell invoking AppendLine multiple times:
Although it appears that the PowerShell function, Get-EnvironmentProperties, returns a string. Result (the return value from Get-EnvironmentProperties) in an array of 10 elements:
The method AppendLine is invoked nine times so the first nine elements of the array. The tenth element of the array (index of 9) is the string return in the last line of function, Get-EnvironmentProperties.
Suppressing the StringBuilder returned by AppendLine results in the the correct behavior, the lone return value is as string as is show below:
A variety of mechanism were show to suppress return value of AppendLine. From the performance stand point, Out-Null is the slowest but from a readability stand point, it is the most readable for all levels of PowerShell developer.
In my code I used the following approach as I learned C as my first programming language:
With regard to performance and suppressing the result of a method/expression StackOverflow has an excellent post on the topic What's the better (cleaner) way to ignore output in PowerShell? A response by JasonMArcher demonstrates and Out-Null has the worst performance.
In this post, we'll explore how to disable the formatOnSave option for specific files, multiple files, using wild cards, and files with certain extensions.
To disable formatOnSave for a specific file, you can add the following setting to your settings.json file:
Replace file path/filename.ext (noted in boldface) with the path and filename of the file for which you want to disable formatOnSave.
To disable formatOnSave for multiple files, you can add the following setting to your settings.json file:
Replace file path/filename1.ext and file path/filename2.ext (noted in boldface) with the path and filenames of the files for which you want to disable formatOnSave.
You can also disable formatOnSave for files that match a specific pattern using wildcards. For example, to disable formatOnSave for files that have a specific prefix, you can add the following setting to your settings.json file:
Replace prefix (noted in boldface) with the desired prefix for the files you want to exclude from formatOnSave.
Similarly, to disable formatOnSave for files that have multiple possible extensions, you can use a wildcard to match the extensions. For example:
Replace file path with the path to the directory containing the files you want to exclude from formatOnSave. Replace ext1 and ext2 with the extensions of the files you want to exclude from formatOnSave.
And that's it! With these settings, you can easily disable the formatOnSave option for specific files, multiple files, or using wildcards.
The modus operandi I use for most of my software-development-related research is to:
Search using Google (yes, I prefer the results to Bing).
Search with keywords and typically target the site that will give me the best results using Google site keyword such as:
2.1 Find the man page for PowerShell's Invoke-RestMethod cmdlet: Invoke-RestMethod site:microsoft.com
. Note above the site:
refers to microsoft.com.
2.2 How to use PowerShell's Where-Object to select a value in an array of arrays: filter array of string arrays PowerShell Where-Object site:stackoverflow.com
. Note above the site:
refers to stackoverflow.com.
Read through and click on the results. For Stack Overflow (I have loved Stack Overflow for the last fifteen years), this is tedious because a lot of results need to be culled to find an answer to your question.
As an experiment, I asked ChatGPT to help me with a piece of code (yes, I could have written this code without searching) but it is complex code. Here are the instructions I gave ChatGPT:
[PSCustomObject[]]
and each element of the array is type [PSCustomObject[]]
. I want to select all values where index 4 equals 'monkey'.[PSCustomObject[]]
and each element of the array is type [string[]]
. I want to select all values where index 4 equals 'monkey'.[string[]] $a = 'x', 'y', 'z'; [string[]] $b = 'e', 'f', 'g'; [string[]] $c = 'q', 'r', 's'; [PSCustomObject[]] $a, $b, $c
. Select all values where array index 1 equals 'f'.enum SegmentIndexes { Region Subregion State }
. Select index [SegmentIndexes]::Region
equals 'f'.Here is the code ChatGPT generated which is correct:
Over the past few years, one of the best people I've worked with is Stephen Durant of Stack Overflow. He is a customer liaison to corporate users of Stack Overflow. I do not normally praise someone for doing their job, but Stephen is really elite at his work. So, Stephen, I apologize because ChatGPT has filled many more of my needs related to technical search.