Powershell activedirectory web services - windows

I am looking to have a webservice that makes a powershell call to add users to active directory. I have the PowerShell call but dont know how to make a webservice to issue the call. Below you will find the powershell script.
New-Item -Path <drive>:\RightsPolicyTemplate\<template_ID>\UserRight -Name <e-mail_address> [-FullControl] [-View] [-Edit] [-Save] [-Export] [-Print] [-Forward] [-Reply] [-ReplyAll] [-Extract] [-AllowMacros] [-ViewRightsData] [-EditRightsData] [-CustomRight <custom_right>,<custom_right>…]
I'm running windows 2008 R2.

You could implement a solution using PHP and HTML forms to call Powershell scripts to do the work. Please see my basic guide on getting started with this here: http://theboywonder.co.uk/2012/07/29/executing-powershell-using-php-and-iis/

Related

Exception calling "DownloadString" with "1' argument<s>

I'm newbie to web development learning by myself. I'm not a cs student. I'm following a book called HTML5 in easy steps. There is a lesson called "Building input forms" which uses free Abyss Personal Edition web server and activeperl scripts.
I want to install activeperl on my machine. I'm on windows 8.1. Activestate site doesn't offer .exe file anymore instead it offers cli installation for windows 10. site says windows 8.1 also be supported.
I tried installing their cli program command for windows which is
powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1')))".
Then I got this error message.[screenshot attached]
screenshot
Then I googled the exception and found that powershell wasn't configured to older versions of windows prior to 2019 to work with tls 1.2.
https://github.com/dotnet/docs/issues/6873
Any tips on how to work around this problem???
Note:
The following is an effective solution for getting the download of the target *.ps1 file to succeed, but additional problems may surface when the successfully downloaded script is executed.
Indeed, since you're using PowerShell v4, script execution failed due to dependence on the v5+ Expand-Archive cmdlet, as discussed in this answer to your follow-up question.
Enable TLS 1.2 as follows, in a ;-separated statement before the .DownloadString() call:
powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; & ([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1')))"
Note: Since you're only using the PowerShell instance for a single download known to use TLS 1.2, the above enables TLS 1.2 only.
If you also had to preserve the originally enabled protocols:
PowerShell v5+ syntax:
powershell -Command "[Net.ServicePointManager]::SecurityProtocol += 'Tls12'; & ([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1')))"
PowerShell v4- syntax:
powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12; & ([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1')))"

Powershell - new-website cmdlet not working when called via web interface

I hope someone can help me with the following :
I’ve created a script to install a web site based on an existing site.
The script performs several steps - copy dirs, restore a DB, create website and application pool etc. It runs fine from the Powershell CLI under an Administrator account.
I created a WinForms frontend for this script that references Powershell and also executes that script just fine.
Now I want to create a web frontend for it. (as explained here : http://devinfra-us.blogspot.com/2011/02/using-powershell-20-from-aspnet-part-1.html)
However I can’t get the ‘new-website’ cmdlet to work via the web frontend. All the other steps in the script work, including ‘new-WebappPool’ (I can see the new appPool in IIS manager). I don’t get any errors..
I’ve made sure the script runs with Administrator rights (security is not a concern at this point)
Below is some output from the Powershell transcript :
**********************
Windows PowerShell transcript start
Start time: 20200106164117
Username: NNN-WEB\adminArr <-- Administrator account
RunAs User: NNN-WEB\adminArr
Machine: NNN-WEB (Microsoft Windows NT 10.0.14393.0)
Host Application: c:\windows\system32\inetsrv\w3wp.exe -ap DefaultAppPool -v v4.0 -l webengine4.dll -a \\.\pipe\iisipmd4bdced8-d455-428b-b9ef-8b3e2bfb38dd -h C:\inetpub\temp\apppools\DefaultAppPool\DefaultAppPool.config -w -m 0 -t 20 -ta 0
Process ID: 8448
PSVersion: 5.1.14393.3383
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.3383
BuildVersion: 10.0.14393.3383
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
You could use the below PowerShell command to create a site in iis:
Import-Module WebAdministration
New-WebAppPool NewAppPool
New-Website -Name "mike" -Port 8086 -PhysicalPath c:\casp -ApplicationPool NewAppPool
Do not forget to refresh the iis after running the command.
Although in my script I also used the commands mentioned in Jalpa's example, I tried his example script anyway. I got the error 'Cannot add duplicate collection entry of type 'site' with unique key attributes 'name, id' respectively set to 'mike3, 2' - which I didn't get using my own script...
Thanks to this error, I figured out that if there are already some existing sites, you also have to specify the 'ID' parameter of the new-website command - so like 'new-website -Name "testsite" -Id 4 etc.'. The ID has to be a number not in use by any other sites.
This made it work :-)

Registering a .NET DLL with Regasm using Powershell fails

With visual studio 2012 I created a DLL file in C# which can be used in Access 2013 by referencing it. This all works fine. I Created the DLL and registered it with CMD doing: RegAsm.exe -tlb -codebase C:\MyFolderX\MyDLL.dll
This all works fine, but because the DLL needs to be registered at multiple computers I wanted to do this with 1 click instead of doing it manually at each user computer.
Because users already use a BATCH file to launch the Access frontend application (which uses the DLL) I thought it would be wise to register it once when they use the BATCH startup. So to do this this I added the following in my BATCH script:
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
RegAsm.exe -tlb -codebase C:\MyFolderX\MyDLL.dll
This sadly doesn't work because it has to be done in admin mode and checking the checkbox run as administrator just jumps through my BATCH code without doing anything for some reason.
So I though, why not use a Powershell script to do the same and launch that from my batch script.
To do this I created the following script:
#Register the assembly
$RegAsm = 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe -codebase -tlb'
$Assembly = 'C:\MyFolderX\MyDLL.dll'
Start-Process $RegAsm $Assembly
pause
This however keeps giving the error:
Start-Process : This command cannot be run due to the error: The system cannot find the file specified. At C:\users\me\Desktop\RegisterMyDLL.pst1
+Start-Process $RegAsm $Assembly
InvalidOperation: (:) Start-Process
FullyQualifiedErrorId : InvalidOperationException, Microsoft.Powershell.Commands.StartProcessCommand
I double checked the location of the DLL and it it just there.. Anyone have a clue what I am doing wrong? Perhaps some syntax error or quote to much? Already tried to escape my backslashes but this didn't had any effect.
Or perhaps there is an better way to achieve easy DLL registering at multiple users?
Does this work?
."C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" -codebase -tlb $Assembly

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.

How to create windows service to run a powershell script?

I have infinite loop PowerShell (Testing purpose) script, which I want to run as a Service in Windows Server 2008 R2 (Standard).
I'm using the following command to create the Windows service,
sc.exe create "My PS1Service" binPath= "powershell.exe -NoLogo -Path D:\TEST\test.ps1"
And the result is [SC] CreateService SUCCESS
But when I try to run the service My PS1Service , it return the following error
Windows Could not srart the My PS1Service service on Local
Computer
Error 1053: The service did not respond to the start or control
request in a family
Any help here !!!
Due to the length of the articles I don't want to paste them in the answer.
Check out this post which is the most comprehensive source of info I've found regarding powershell services: https://msdn.microsoft.com/en-us/magazine/mt703436.aspx
which links to an example powershell service:
http://jf.larvoire.free.fr/progs/PSService.ps1
I didn't end up successfully modifying this as it was going to take me longer to read through it and understand it than it would to use a different method for my use case, but I'd certainly be going back to that if I needed to create a service in powershell in the future.
Also this: https://www.sapien.com/blog/2017/07/12/write-a-windows-service-in-powershell/
I've been looking all over the place for a decent way to make a PS1 into a service.
Easiest way I found is with Winsw:
https://github.com/kohsuke/winsw
Works like a charm.
I have had success with Non-sucking Service Manager (NSSM) as well. https://nssm.cc/
you can create by below step:
First go to the path where PowerShell script located.
Then run below Command:
powershell.exe -ExecutionPolicy UnRestricted -File .\filename.ps1

Resources