PowerShell DSC. Download and install software - powershell

Is it possible to download some software from internet, then install it on some of my servers using DSC? Chrome, for example? All DSC tutorials are pretty hard to understand (at least for me). I just want to see a simple example, similar to my use case, please.

You can also use DSC to install a package from the internet via a URL without Chocolatey. To do so, you need the exact name the product will be installed as and its ProductId value. The easiest way to get these is to install the software somewhere manually once first, then find these values via this PowerShell command:
Get-WmiObject Win32_Product | Format-Table IdentifyingNumber, Name, Version
Then you can install the software via DSC by using via the Package resource. Here's an example of doing so with the Local Administrator Password Solution tool from Microsoft:
Package 'LAPS' {
Name = 'Local Administrator Password Solution'
Path = 'https://download.microsoft.com/download/C/7/A/C7AAD914-A8A6-4904-88A1-29E657445D03/LAPS.x64.msi'
ProductId = 'EA8CB806-C109-4700-96B4-F1F268E5036C'
}

Yes it is possible to use DSC to do what you desire. Here is an example of using Chocolatey community resource to install Chrome https://github.com/PowerShellOrg/cChoco/blob/master/ExampleConfig.ps1

Related

Powershell silent installation issue

What will happen if we try to install a software using powershell which has already installed in a server . For an example I already have notepad++ in my server, Now I try to install same notepad++ version in my server using powershell. Then what will be the output?. Besides, Is there a way that I could find whether a software has already installed in server or not.
There are many kinds of installers, but most add records in the Add / Remove list of programs, but there are no guarantees. Here is C++ code to scan the registry and check via WMI. You can use scripts instead of course, but it is not an exact science to find what is installed - some installers are very custom and non-standard and follow few guidelines.
Registry Entries:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
MSI Packages:
For MSI packages there are ways to check whether the exact same version or a related version is installed. If you have the product code of the MSI, you can simply check whether it is installed like this:
Dim installer : Set installer = CreateObject("WindowsInstaller.Installer")
MsgBox installer.ProductState("{00000000-0000-0000-0000-000000000001}") ' <= PRODUCT CODE
Longer sample linked here.
You can find the product code for an installed MSI using several approaches: How can I find the product GUID of an installed MSI setup?
If you have the upgrade code for a family of MSIs you can use the RelatedProducts method to find out whether a related product is installed:
Set installer = CreateObject("WindowsInstaller.Installer")
Set upgrades = installer.RelatedProducts("{UPGRADE-CODE-GUID-HERE}")
For Each u In upgrades
MsgBox u, vbOKOnly, "Product Code: "
Next
How can I find the Upgrade Code for an installed MSI file?. You can get the upgrade code for an MSI due to be installed by looking in the Property table using Orca.
Pragmatic Approaches:
One option is to identify a key file from each installation and check for its existence using any language you want - scripting will do.
Set fso = CreateObject("Scripting.FileSystemObject")
MsgBox fso.GetFileVersion("C:\Windows\System32\vcruntime140.dll")
The above script snippet from this rant on how to find the installed VCRedist version.
Link:
Adding entries to MSI UpgradeTable to remove related products

How to install GroupPolicy module for PowerShell?

I just started writing scripts in PowerShell.
The desired script is supposed to create a local GroupPolicyObject (GPO) which will be specified afterwards. Research showed that it could be done with the New-GPO command within the GroupPolicy module for PowerShell. I tried to install the mentioned module but unfortunately nothing I found worked. May I ask for help?
I am using Windows 7 and Powershell 5.1.14409.1005
Error I receive when running example from Microsoft page (New-GPO -Name TestGPO -Comment "This is a test GPO."):
It is actually extremely simple, but the organization of the modules and features has changed many times in the past ten years, Microsoft Docs don't document the up-to-date way anywhere and all articles found online are very old.
Anyway, to install the Powershell Module called GroupPolicy, you need to install the Windows Feature called GPMC (Group Policy Management Console) which includes the mentioned module.
Install-WindowsFeature GPMC
This feature was once a subfeature of the RSAT feature, which is the cause of confusion.
Step 1. Install RSAT from Microsoft site:
https://www.microsoft.com/en-in/download/details.aspx?id=7887
Step 2. Enable Group Policy from Windows Features.
Following link describes the steps in details:
https://www.powershellmagazine.com/2012/05/14/managing-group-policy-with-powershell
Have you installed the RSAT (Remote Server Administration Tools) Group Policy component?

Getting Install path of a package just installed by chocolatey in powershell

After I install a package in powershell by using
"choco install $package" where package is taken from a config file and would look like "WinRar" so I would be doing choco install WinRar, how do i get the exact path this package was just installed to?
For example when I am installing PhantomJS using this, it gets installed to C:\ProgramData\chocolatey\lib\PhantomJS\tools\phantomjs-2.1.1-windows and I as the developer know that, but since I need to add this to the env path, depending on which version the install command installs, the path will be different. I need to get the exact path so i can set the environmental variable to right place.
PhantomJS is just one example, but a lot of packages get installed into directories where their version is apart of the path and getting the path from the powershell install scripts would really be helpful.
Is there anything like this available for the package manager? I assume figuring out where the package just got installed to should be possible because I see it displayed on my terminal window, just don't know how to access it in powershell.
Thanks.
Currently there is not a way, but there is a thought to maybe provide back a list of package results with that information (along with more). That is still in a feature request so look for it to be developed in the coming months.
You could parse the Chocolatey output to determine where Chocolatey saw things get installed and we are working to make that detection even better.

How to create and install a powershell command package using chocolatey?

I have a few powershell commands in a script that I want to distribute to everyone on my team. This script may be updated and I would want an easy way for my team to update this script as well. Someone suggested Chocolatey which I have never used before. I found the following in the Chocolatey.org FAQ:
"What kind of package types does Chocolatey support?
Binary Packages – Installable/portable applications – This is 98% of the Chocolatey packages – most are pointers to the real deal native installers and/or zipped software.
PowerShell Command Packages – Packages that have the suffix .powershell will install PowerShell scripts as commands for you to call from anywhere.
Development Packages – Packages that have the suffix .dev. For instance dropkick.dev.
Coming soon – Virtual Packages – Packages that are like a category, and you just want one package from that category. Read more ..."
Does anyone have an example of using chocolatey to install a powershell script to the Path so that the commands in it can be executed from anyway on the machine? I am unable to find an example of how to do this online.
If this is an inappropriate use of chocolatey, please let me know and feel free to recommend an alternate solution.
Thank you very much for your time. Please let me know if I am being unclear or if you have any questions for me?
Look at the PowerShell Function Reference, which has all of the different functions you can call. Then take a look, specifically, at the Install-ChocolateyPowershellCommand helper. Here's an example of a package that installs a powershell script as a command (source).

How to install Azure cmdlets using powershell

I'm trying to install Azure cmdlets using powershell, not the wizard provided by Microsoft.
That's because my script (which has Azure cmdlets) will be used in a new virtual machine located in Azure and if my script try to run some cmdlet of Azure, will fail for sure.
I would like to put the installation lines of the powershell cmdlets on the top of my script for install the whole cmdlets and after that, that my script execute the other cmdlets without problem.
So, anyone knows?
Thanks!
If you have the Web Platform Installer in the VM, you can use the script I posted at PowerShell Magazine.
http://www.powershellmagazine.com/2014/02/27/using-powershell-and-web-platform-installer-to-install-azure-powershell-cmdlets/
Or get the Windows Standalone installer from https://github.com/Azure/azure-sdk-tools/releases and use msiexec to install that.
If you want to use PowerShell to download the latest version too:
You can use Invoke-WebRequest to read the page (https://github.com/Azure/azure-sdk-tools/releases) and then get all links from that. You can then get all links that end with .msi and take the first link for download.
#Code not tested
$doc = Invoke-WebRequest 'https://github.com/Azure/azure-sdk-tools/releases'
$links = $doc.Links.Href