"Install-Module -name AzureAD" fails with error on PS6 with MacOSX - macos

I was able to install Powershell Core on MacOSX and now I'm trying to administer an Azure AD instance from the same.
When I install the AzureAD module, I get the following error
PS /Users/c> Install-Module -name AzureAD
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"):y
PackageManagement\Install-Package : Unable to load DLL 'api-ms-win-core-sysinfo-l1-1-0.dll': The specified module or one of its dependencies could not be found. (Exception from HRESULT: 0x8007007E) At /usr/local/microsoft/powershell/6.0.2/Modules/PowerShellGet/1.6.0/PSModule.psm1:2057 char:21 + ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : System.DllNotFoundException,Microsoft.PowerShell.Commands.TestModuleManifestCommand,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
I looked around for ways to get api-ms-win-core-sysinfo installed on Mac, but being it's a different OS, I didn't see any straightforward resolutions.

This module is not yet ported to dotnet core, so you cant use it on Linux\Mac.
You can take a look here:
https://www.powershellgallery.com/packages/AzureAD/2.0.1.6
also, you can use this search to confirm my statement:
https://www.powershellgallery.com/items?q=Tags%3A%22PSEdition_Core%22++azuread&x=0&y=0

As #4c74356b41 rightly says, the Azure AD module is not ported to PowerShell core yet.
Besides installing a VM with windows, and depending exactly what you wish to do, you could try the Azure CLI for OS X
If a browser experience is okay for you, the Azure Cloud Shell also offers CLI and even some Powershell commands (though this also runs on Core in the background):

If you look at the error message it is clear, that the error is not about the AzureAD module not being available on your platform, but more about the "PackageManagement"-Package not being supported. So you can try downloading the module directly from powershellgallery and unzipping it into your module path.

I've hit the same problem - it's frustrating that the AzureAD module isn't open-sourced on GitHub. While the Az module is, it lacks most of the functionality of the AzureAd module.
One option is to "copy" the version of AzureAD from cloud shell, as outlined here:
https://www.michev.info/Blog/Post/2339/azure-ad-powershell-module-with-support-for-powershell-core
Another option is to write your own Powershell core module that calls the Graph REST APIs directly.

Related

Install PSWindowsUpdate Module Issue

enter image description here
Recently we want to update security and critical patches using command for customer's windows domain servers, and the PSWindowsUpdate Module seems is very suitable.
However, we tried to install it via "Install-Module -Name PSWindowsUpdate -Force" and also allow those servers can access Internet URL(.microsoft.com/.powershellgallery.com etc), but it still failed as screenshot
We google some links and use TLS1.2, however it still can't work and that very confused us that "WARNING: Source Location 'https://www.powershellgallery.com/api/v2/package/PSWindowsUpdate/2.2.0.3' is not valid."
I also tried to do that on my laptop with same method, it can work perfectly and there is no any error on powershell.
Customer windows server is Win2019 Datacenter and Standard
Customer powershell version is 5.1.17763.2931
We have no idea on the difference and how to install, any suggestion?
You may try the following command before installing PSWindowsupdate
Install-PackageProvider -name NuGet -MinimumVersion 2.8.5.201 -Force

Execute bash commands on Azure Linux VM using azure automation

I need to install software on Azure VM with Ubuntu image. One way of manually doing it is ssh into the terminal using putty or other command-line tool and executing the installation bash command. Another way of doing it was by automating this by creating a bash script and calling it from Azure automation. But I unable to find a way to call the bash script from the azure automation runbooks. Kindly let me know if this is possible.
I am able to call powershell scripts because Azure Automation supports powershell by default.
I tried using the command New-SshSession and using Invoke-SshCommand to run my command but I get the below error while trying to connect to a session.
Also, what are the prerequisites for using New-SshSession (see below)?
New-SshSession -ComputerName 'ComputerName' -Username 'UserName'
Unable to create SSH client object: Exception calling ".ctor" with "4" argument(s): "Could not load type 'System.Security.Cryptography.HMACRIPEMD160' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral,
Are there any prerequisites before I should start using New-SshSession? Do I need to install any .Net library on my linux machine?
Welcome to Stack Overflow!!
If the requirement was for Windows VM then it would have been easy by leveraging Invoke-AzVmRunCommand cmdlet but as requirement is for Linux VM so looks like the comment (with sample script) provided by Azure Automation team content author in this GitHub helps you to accomplish your requirement of executing bash commands on Azure Linux VM using azure automation.
Hope this helps!!

Docker for Windows - Install certificates using sn.exe / Strong Name utility

We have a Docker image that we use to build our Visual Studio solutions. This works great. Now we have some solutions that require a key. To install the key you use sn.exe, but Microsoft has gone through great lengths to make sure human input is needed, namely the password..
The image is based off FROM microsoft/dotnet-framework:3.5
I tried several tricks, especially from this Stack Overflow thread: Auto-entering Password In Sn.exe
The last answer is mine (Thomas Rijsewijk). At least I have a working way to install the key automatically, but somehow SendWait doesn't work in Docker, or Docker for Windows, or microsoft/dotnet-framework:3.5 docker image.
# ---------
# Import all certificates in C:\keys
# ---------
[void][System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("hello")
When I run this I get "Access Denied":
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At K:\install-certificates.ps1:51 char:1
+ [System.Windows.Forms.SendKeys]::SendWait("hello")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Win32Exception
As an alternative I tried SendKeys() from WScript.Shell:
$wshell = New-Object -com wscript.shell;
Sleep 5;
$wshell.sendkeys("test");
Again, this works perfectly on my machine (Windows 10 up to date) and an up to date Windows 2016 server. But NOT inside the docker image: nothing happens, no error but it's not entering "test" either.
Lot of talks about automating powershell, but basically I don't really care HOW I install the certificate when building my docker image, I just want it installed. I install the certificate locally using
Start-Process "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\sn.exe" -ArgumentList "-i `"D:\key.pfx`" VS_KEY_XXXXXXXX" -NoNewWindow;
At this point, it asks for a password which makes it impossible to use with docker build.
I did a whole lot of searching on Google and SO, I'm surprised to see that nobody else tried to install a key using sn.exe.
PS: Yes, I know VS_KEY_XXXXXXXX is not a valid VS_KEY. I already have a working mechanism for extracting the right VS_KEY, but that's out of scope of this question.
PPS: Yes, I know I could manually running the docker, install the certificate and manually commit and push the changes. But naturally, I want it to originate from my Dockerfile

Creating Azure self-signed sertificate with powershell Invoke-AddCertToKeyVault command

Recently, I tried to create self-signed certificate for Azure Service Factory accordingly with Microsoft's manual:
Azure Docs: Secure a Service Fabric cluster, step 2.5, 02/05/2016
But command Invoke-AddCertToKeyVault failed with the next error:
Invoke-AddCertToKeyVault : The term 'Invoke-AddCertToKeyVault' is not
recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
I think that Azure Powershell successfully installed on my machine because I was able to login into my Azure account by running Login-AzureRmAccount. Also $env:PSModulePath says that Azure Modules path added to path variable (accordingly with the article: Azure Docs: How to install and configure Azure PowerShell, 04/22/2016). Here they are:
...\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\;
...\Microsoft SDKs\Azure\PowerShell\ServiceManagement\;
...\Microsoft SDKs\Azure\PowerShell\Storage\;
Also, I have restarted my PC after installing Azure PowerShell.
It is possible that I have missed something, but I am stuck with it. How could it be resolved?
That commandlet is in the package that should be imported -
Import-Module "C:\Users\chackdan\Documents\GitHub\Service-Fabric\Scripts\ServiceFabricRPHelpers\ServiceFabricRPHelpers.psm1"
That is its implementation, by the way, for a reference that it even exists :). Try Import-Module and it should work.

Azure xplat to run a CustomScriptExtension in a Windows VM

I am creating Windows VMs from the azure xplat cli, using the following command:
azure network vnet create --location "East US" testnet
azure vm create --vm-name xplattest3 --location "East US" --virtual-network-name testnet --rdp 3389 xplattest3 ad072bd3082149369c449ba5832401ae__Windows-Server-Remote-Desktop-Session-Host-on-Windows-Server-2012-R2-20150828-0350 username SAFEpassword!
After the Windows VM is created I would like to execute a powershell script to configure the server. As far I understand, this is done by executing a CustomScriptExtension.
I found several examples for PowerShell but no examples for Xplat cli.
I would like, for example, to run the following HelloWorld PowerShell script:
New-Item -ItemType directory -Path C:\HelloWorld
After reading documentation I should be able to run a CustomExtensionScript by executing something like this (the following command does not work):
azure vm extension set xplattest3 CustomScriptExtension Microsoft.Compute 1.4 -i '{"URI":["https://gist.githubusercontent.com/tk421/8b7dd37145eaa8f82e2f/raw/36c11aafd3f5d6b4af97aab9ef5303d80e8ab29b/azureCustomScriptExtensionTest"] }'
I think that the problem is the parameter -i. I have not been able to find an example on Internet. There are some references and documentation such as MSDN and Github, but no examples.
Therefore, my question: How to execute a PowerShell script after creating a Windows VM in Azure using the xplat cli ?
Please note that the my current approach is a CustomScriptExtension, but anything that allows to bootstrap a configuration script will be considered!
EDIT How do I know it is failing ?
After I run the command azure vm extension ...:
xplat cli confirms that the command has been executed properly.
As per MSDN documentation, the folder C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\ is created, but there is no script downloaded to C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\{version-number}\Downloads\{iteration}
The folder C:\HelloWorld is not created, which means that the contents of the script has not been executed.
I cannot find any sort of logs or a trace to know what happened. Does anyone knows where can I find this information ?
The parameters (The Json) that I used after reading the MSDN documentation were not correct. However, you can get clues of the correct parameters by reading the C# code.
And the final command is:
azure vm extension set xplattest3 CustomScriptExtension Microsoft.Compute 1.4 -i '{"fileUris":["https://macstoragetest.blob.core.windows.net/testcontainername/createFolder.ps1"], "commandToExecute": "powershell -ExecutionPolicy Unrestricted -file createFolder.ps1" }'
This command successfully creates the C:\HelloWorld directory.
NOTE: I decided to upload the script to Azure as I read in a post and in the documentation that is mandatory. However I just made a test to download the original script from Github and it is working fine, so I guess that the documentation is a bit outdated.
EDIT: I created an detailed article that explains how to provision windows servers with xplat-cli in Azure.

Resources