Get multiple IIS websites .Net CLR version via powershell - windows

I am trying to get the .Net CLR version of each application pool with their respective virtual directories.
I have the below script which returns the website and its app pool with no issues, however I can't figure out how to use the "Get-IISAppPool" in the below snippet for me to return it's .Net CLR version as well.
$Websites = Get-IISSite "mylocalsite" #if we want to specify only 1 website
$AllVDirs = #()
foreach($Site in $Websites)
{
$VDirs = Get-WebApplication -Site $Site.Name
foreach($webvdirectory in $VDirs)
{
$vdir = New-Object psobject -Property #{
"Name" = ($webvdirectory.path -split "/")[-1]
"Site" = $Site.name
"Path" = $webvdirectory.path
"App Pools" = $webvdirectory.applicationPool
"Physical Path" = $webvdirectory.physicalPath
#"PhysicalPathCredentials" = $webvdirectory.userName
}
$AllVDirs += $vdir
}
}
$AllVDirs

Related

How do we add DefaultRequestVersion on an HttpClient object in Powershell?

I have been trying a way to add the Http Version as 1.0 in Powershell to my HttpClient Object.
function Post-JSONData
{
Param
(
[Parameter(Mandatory=$True)] [String] $JSONPayload,
[Parameter(Mandatory=$True)] [String] $ObjectClass,
[Parameter(Mandatory=$True)] [String] $APIUrl,
[Parameter(Mandatory=$False)] [String] $ProxyUrl
)
#Try{
If($JSONPayload.StartsWith("[") -eq $false -and $JSONPayload.EndsWith("]") -eq $false)
{ $JSONPayload = "[" + $JSONPayload + "]" }
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Add-Type -AssemblyName System.Net.Http
$WebHandler = New-Object System.Net.Http.HttpClientHandler
$WebHandler.AllowAutoRedirect = $false;
If($ProxyUrl)
{
$WebProxy = New-Object System.Net.WebProxy($ProxyUrl)
$WebHandler.Proxy = $WebProxy
}
$HttpClient = New-Object System.Net.Http.HttpClient($WebHandler)
**$HttpClient.DefaultRequestVersion = [System.Net.HttpVersion]::Version10**
$HttpClient.DefaultRequestHeaders.Add("Accept","*/*");
$HttpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
$HttpClient.DefaultRequestHeaders.Add("Connection","keep-alive");
$HttpClient.DefaultRequestHeaders.Add("Class",$ObjectClass);
$HttpClient.Timeout = New-Object System.TimeSpan(0, 0, 90);
$HttpJSONPayload = New-Object System.Net.Http.StringContent($JSONPayload.ToString(), [System.Text.Encoding]::UTF8, "application/json")
$HttpJSONPayload.Headers.ContentEncoding.Add("gzip")
$HttpJSONPayload.Headers.ContentEncoding.Add("deflate")
$HttpJSONPayload.Headers.ContentEncoding.Add("br")
$ResponsePayload = $HttpClient.PostAsync([Uri] $APIUrl,$HttpJSONPayload)
I am able to add the DefaultRequestHeader Parameters but I believe there is some issue with the format for DefaultRequestVersion.
I am unable to find documentation online either for the same.
Update: I am using Powershell V4.0 so $HttpClient.DefaultRequestVersion = [System.Net.HttpVersion]::Version10 is giving the following error:
The property 'DefaultRequestVersion' cannot be found on this object. Verify that the property exists and can be set.
The DefaultRequestVersion property is not a list, but a single HttpVersion value.
To default to HTTP 1.0, assign it like this:
$HttpClient.DefaultRequestVersion = [System.Net.HttpVersion]::Version10
Beware that the DefaultRequestVersion property was only introduced in .NET Core 3.0, so the earliest version of PowerShell with this property exposed is PowerShell 7.0.0
For PowerShell 4.0, you can change your code slightly to support overriding the HTTP Version by manually crafting the request and calling SendAsync() directly (instead of PostAsync()):
Take this line:
$ResponsePayload = $HttpClient.PostAsync([Uri] $APIUrl,$HttpJSONPayload)
... and replace it with:
# Manually craft the request message and overwrite the version
$RequestPayload = New-Object System.Net.Http.HttpRequestMessage #([System.Net.Http.HttpMethod]::Post, [uri]$APIUrl)
$RequestPayload.Version = '1.0'
$RequestPayload.Content = $HttpJSONPayload
# Pass the crafted message directly to SendAsync()
$ResponsePayload = $HttpClient.SendAsync($RequestPayload)

Delete Security Support Package

so im working on a powershell script to manage security Support Providers for specialized controls. currently i have the script working to ADD a SSP, but when i try and change the script to DELETE a ssp, it breaks.
Here is the code:
$DynAssembly = New-Object System.Reflection.AssemblyName('SSPI2')
$AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SSPI2', $False)
$TypeBuilder = $ModuleBuilder.DefineType('SSPI2.Secur32', 'Public, Class')
$PInvokeMethod = $TypeBuilder.DefinePInvokeMethod('DeleteSecurityPackage',
'secur32.dll',
'Public, Static',
[Reflection.CallingConventions]::Standard,
[Int32],
[Type[]] #([String]),
[Runtime.InteropServices.CallingConvention]::Winapi,
[Runtime.InteropServices.CharSet]::Auto)
$Secur32 = $TypeBuilder.CreateType()
$RuntimeSuccess = $True
$Result = $Secur32::DeleteSecurityPackage($DllName)
Every time i run this i get: Exception calling "DeleteSecurityPackage" with "1" argument(s): "The function requested is not supported
however this piece of code to ADD the ssp works fine:
$DynAssembly = New-Object System.Reflection.AssemblyName('SSPI2')
$AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('SSPI2', $False)
$TypeBuilder = $ModuleBuilder.DefineType('SSPI2.Secur32', 'Public, Class')
$PInvokeMethod = $TypeBuilder.DefinePInvokeMethod('AddSecurityPackage',
'secur32.dll',
'Public, Static',
[Reflection.CallingConventions]::Standard,
[Int32],
[Type[]] #([String], [IntPtr]),
[Runtime.InteropServices.CallingConvention]::Winapi,
[Runtime.InteropServices.CharSet]::Auto)
$Secur32 = $TypeBuilder.CreateType()
if ([IntPtr]::Size -eq 4) {
$StructSize = 20
} else {
$StructSize = 24
}
$StructPtr = [Runtime.InteropServices.Marshal]::AllocHGlobal($StructSize)
[Runtime.InteropServices.Marshal]::WriteInt32($StructPtr, $StructSize)
$RuntimeSuccess = $True
$Result = $Secur32::AddSecurityPackage($DllName, $StructPtr)
by rights it should be easier to delete since i dont need t worry about the struct, however it is not happy.
any help would be appreciated
It seems like this was not fully implemented/supported by Microsoft. This article seems to support that: http://cybernigma.blogspot.com/2014/03/using-sspap-lsass-proxy-to-mitigate.html The relevant information is about 3/4 of the way down.

How to filter Microsoft Service using PowerShell

How to filter the services that belong to Microsoft [including applications such as Office, IIS, AD, EXCH and so on] using PowerShell [v1.0 preferably].
In the "msconfig.msc -> Services Tab" there is a check box that reads "Hide all Microsoft Services", I'm looking for the same functionality of filtering the services that belong to MS.
Is this a possibility? Kindly assist.
This will give you a way to start:
Get-WmiObject Win32_Service -Property * | Select DisplayName,PathName | %{ Try { if([System.Diagnostics.FileVersionInfo]::GetVersionInfo("$($_.PathName.ToString().Split("-")[0].Split("/")[0])").LegalCopyright -like "*Microsoft*") {"$($_.DisplayName) is a Microsoft Service"}} catch {}}
I used the code above to make this. I was interested in all the non-MS services and a more versatile output format.
$services = Get-WmiObject Win32_Service -Property Name,DisplayName,PathName | Select
Name, DisplayName,PathName
$serviceList = New-Object System.Collections.ArrayList
foreach ($service in $services) {
Try {
$path = $service.Pathname.tostring().replace('"','')
$cri = ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($path)).legalcopyright
if ($cri -notlike "*Microsoft*") {
$serviceList += $service
}
} catch {}
}
$serviceList

Get latest from Visual Studio Team Services using Command Line passing /login credentials with TF.exe

Has anyone had success getting latest source code from the Visual Studio Team Services (formerly Visual Studio Online, Team Foundation Service) Version Control Server using the command line and passing in credentials programmatically?
-I have discovered that you can't use the Windows ID credentials that you use to login to Team Explorer or the VSO website in the command line. You need to create Alternate Credentials for the user profile in Team Services.
-I have found out that if you omit the /login in tf.exe, the Team Services login dialog appears and asks you to type in your Windows ID credentials (unless they are already cached in your Team Explorer or Visual Studio (or even possibly Browser and Windows Credential Caches)
-I have found out that the alternate credential work Using the Java version of tf.exe - Team Explorer Everywhere Command Line Client (TEE CLC). TEE CLC actually uses the /login credentials that you pass in and lets you connect. The same thing does NOT seem to be possible with the TF.EXE in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ BUT INSTALLING JAVA ON THIS BUILD ENVIRONMENT IS AGAINST POLICY. So the TEE CLC is NOT a viable option.
tf get $/MyProj /collection:https://myaccount.visualstudio.com/DefaultCollection /login:user:pass
the above command simply ignores the /login credentials if you have the Windows ID credentials cached or it returns the error message TF30063: You are not authorized to access myaccount.visualstudio.com (which is not true, because the credentials DO work with the Java client)
Are there any other alternatives that do not require installing Java?
I got an answer from Microsoft Support: AA Creds for VSO do not work with TF.EXE at this time. TEE CLC or using object model code are the only alternatives currently. We are looking at doing this in the future.
Object Model Code refers to the Microsoft.TeamFoundation.VersionControl.Client Namespace in the dll by the same name. I ended up writing a quick C# console app to download the latest code without installing Java. An added benefit of this approach is that it does not require creating a throwaway Workspace.
if you use the code below to create an executable called tfsget.exe it can be called from the command line like this:
tfsget https://myvso.visualstudio.com/DefaultCollection $/MyProj/Folder c:\Projects login password
and I added a silent switch to suppress listing each file that can be used like:
tfsget https://myvso.visualstudio.com/DefaultCollection $/MyProj/Folder c:\Projects login password silent
here's the code, hope this helps until MS updates TF.exe
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace TfsGet
{
class Program
{
static void Main(string[] args)
{
var tfsParams = TfsDownloadParams.Create(args);
var tpc = new TfsTeamProjectCollection(new Uri(tfsParams.ServerUrl), tfsParams.Credentials);
CheckAccess(tpc, tfsParams);
Download(tpc, tfsParams);
}
private static void CheckAccess(TfsTeamProjectCollection tpc, TfsDownloadParams tfsParams)
{
try
{
tpc.Authenticate();
}
catch
{
Console.WriteLine("TFS Authentication Failed");
Console.WriteLine("Server Url:{0}", tfsParams.ServerUrl);
Console.WriteLine("Project Path:{0}", tfsParams.ServerProjectPath);
Console.WriteLine("Target Path:{0}", tfsParams.TargetPath);
Environment.Exit(1);
}
}
static void Download(TfsTeamProjectCollection tpc, TfsDownloadParams tfsParams)
{
var versionControl = tpc.GetService<VersionControlServer>();
// Listen for the Source Control events.
versionControl.NonFatalError += Program.OnNonFatalError;
var files = versionControl.GetItems(tfsParams.ServerProjectPath, VersionSpec.Latest, RecursionType.Full);
foreach (Item item in files.Items)
{
var localFilePath = GetLocalFilePath(tfsParams, item);
switch (item.ItemType)
{
case ItemType.Any:
throw new ArgumentOutOfRangeException("ItemType.Any - not sure what to do with this");
case ItemType.File:
if (!tfsParams.Silent) Console.WriteLine("Getting: '{0}'", localFilePath);
item.DownloadFile(localFilePath);
break;
case ItemType.Folder:
if (!tfsParams.Silent) Console.WriteLine("Creating Directory: {0}", localFilePath);
Directory.CreateDirectory(localFilePath);
break;
}
}
}
private static string GetLocalFilePath(TfsDownloadParams tfsParams, Item item)
{
var projectPath = tfsParams.ServerProjectPath;
var pathExcludingLastFolder = projectPath.Substring(0, projectPath.LastIndexOf('/')+1);
string relativePath = item.ServerItem.Replace(pathExcludingLastFolder, "");
var localFilePath = Path.Combine(tfsParams.TargetPath, relativePath);
return localFilePath;
}
internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
{
var message = e.Exception != null ? e.Exception.Message : e.Failure.Message;
Console.Error.WriteLine("Exception: " + message);
}
}
public class TfsDownloadParams
{
public string ServerUrl { get; set; }
public string ServerProjectPath { get; set; }
public string TargetPath { get; set; }
public TfsClientCredentials Credentials { get; set; }
public bool Silent { get; set; }
public static TfsDownloadParams Create(IList<string> args)
{
if (args.Count < 5)
{
Console.WriteLine("Please supply 5 or 6 parameters: tfsServerUrl serverProjectPath targetPath userName password [silent]");
Console.WriteLine("The optional 6th 'silent' parameter will suppress listing each file downloaded");
Console.WriteLine(#"Ex: tfsget ""https://myvso.visualstudio.com/DefaultCollection"" ""$/MyProject/ProjectSubfolder"" ""c:\Projects Folder"", user, password ");
Environment.Exit(1);
}
var tfsServerUrl = args[0]; //"https://myvso.visualstudio.com/DefaultCollection";
var serverProjectPath = args[1]; // "$/MyProject/Folder Path";
var targetPath = args[2]; // #"c:\Projects\";
var userName = args[3]; //"login";
var password = args[4]; //"passsword";
var silentFlag = args.Count >= 6 && (args[5].ToLower() == "silent"); //"silent";
var tfsCredentials = GetTfsCredentials(userName, password);
var tfsParams = new TfsDownloadParams
{
ServerUrl = tfsServerUrl,
ServerProjectPath = serverProjectPath,
TargetPath = targetPath,
Credentials = tfsCredentials,
Silent = silentFlag,
};
return tfsParams;
}
private static TfsClientCredentials GetTfsCredentials(string userName, string password)
{
var networkCreds= new NetworkCredential(userName, password);
var basicCreds = new BasicAuthCredential(networkCreds);
var tfsCreds = new TfsClientCredentials(basicCreds)
{
AllowInteractive = false
};
return tfsCreds;
}
}
}
Here is a snippet using .NET TFS libraries & Powershell integration that worked for us - tf.exe does not play well with VSO authorization. Curious if anyone else has success using this route. We are using ADFS, so the powershell process is run as the user we want to authenticate with.
TF Get Latest Workspace.ps1
$path = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
Add-Type -Path "$path\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$path\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$path\Microsoft.TeamFoundation.VersionControl.Common.dll"
$collection = "https://mycorp.visualstudio.com/defaultcollection"
$tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($collection)
$vc = $tpc.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
# retrieve workspace by path
$projectPath = "$/MyApp/MyBranch"
$workspacePath = "C:\Projects\MyApp\MyBranch"
$workspace = $vc.GetWorkspace($workspacePath)
# get full download every time (tf get /force /recursive)
function DownloadAllFiles([Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspace, [string] $projectPath) {
$recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$versionSpec = [Microsoft.TeamFoundation.VersionControl.Client.LatestVersionSpec]::Instance
$itemSpec = new-object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($projectPath,$recursionType)
$getRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($projectPath,$recursionType,$versionSpec)
$getOptions = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite
$workpaceStatus = $workspace.Get($getRequest, $getOptions)
write-output $workpaceStatus
}
# get delta download - changes only (retrieves all mapped items)
function DownloadWorkspaceUpdates([Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspace) {
$workpaceStatus = $workspace.Get()
write-output $workpaceStatus
}
# get delta download - changes only (retrieves specific mapped items)
function DownloadWorkspaceUpdates([Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspace, [string] $projectPath) {
$recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$versionSpec = [Microsoft.TeamFoundation.VersionControl.Client.LatestVersionSpec]::Instance
$itemSpec = new-object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($projectPath,$recursionType)
$getRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($projectPath,$recursionType,$versionSpec)
$getOptions = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite
$workpaceStatus = $workspace.Get($getRequest, $getOptions)
write-output $workpaceStatus
}
# force latest download (slower)
DownloadAllFiles -workspace $workspace -projectPath $projectPath
# download deltas (fast), all mappings
DownloadWorkspaceUpdates -workspace $workspace
# download deltas (fast), specific mapping
DownloadWorkspaceUpdates -workspace $workspace -projectPath $projectPath
This could easily be extended to support TfsClientCredentials (i.e. Alternate Access credentials). I prefer the powershell approach as it doesn't require compilation or an extra EXE to copy around.

powershell windows form browes dialogue

Im trying to create a windows form that has a button when clicked will display a folder/file browes window, then the user selects the file/folder and clicks OK and i can then use the selected path as a string for another script.
the problem is that when i run it through PowerGUI (powershell scripting app) it works fine, but when i run through windows powershell it hangs when loading the browse dialog, anyone seen this before or see what ive done wrong or got an alternative, any help would be appreciated.
cls
$button = $browse = $form = 0
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$browse = new-object system.windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.selectedPath = "C:\"
$browse.Description = "Choose a directory"
$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Choose Directory"
$button1.Add_Click({$browse.ShowDialog()})
$button1.left = 20
$button1.top = 20
$form = New-Object system.windows.forms.Form
$form.controls.add($button1)
$form.ShowDialog()
$form.Dispose()
$browse.SelectedPath
I was having a similar problem when running my script through PowerShellPlus (anther powershell editor). Luckily I found this post that shows how to prompt for a folder without using the FolderBrowserDialog. Here's the code that I'm using in a set of powershell functions I've written for prompting the user for many different kinds of input via a GUI.
# Show an Open Folder Dialog and return the directory selected by the user.
function Read-FolderBrowserDialog([string]$Message, [string]$InitialDirectory)
{
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, 0, $InitialDirectory)
if ($folder) { return $folder.Self.Path } else { return '' }
}
Your code works when I try it. However I have noticed that sometimes(especially the 2nd time in a session) I use a browsewindow, it is hidden behind the PowerShell console and it seems like it's stuck. So can you try moving your powershell console to the side when it "hangs"?
Also, as a suggestion: if you're only using the form to select a folder location, I would skip it. You won't recieve the browser value until you close the form anyways, so try something like this instead:
function Get-BrowseLocation
{
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()
$browse = New-Object System.Windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.Description = "Choose a directory"
$loop = $true
while($loop)
{
if ($browse.ShowDialog() -eq "OK")
{
$loop = $false
} else
{
$res = [System.Windows.Forms.MessageBox]::Show("You clicked Cancel. Try again or exit script?", "Choose a directory", [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
if($res -eq "Cancel")
{
#End script
return
}
}
}
$browse.SelectedPath
$browse.Dispose()
}
PS > Get-BrowseLocation
D:\
If you make the following changes to the function provided by Frode. F, the dialog will always come to the top.
$topform = New-Object System.Windows.Forms.Form
$topform.Topmost = $true
$topform.MinimizeBox = $true
$loop = $true
while($loop)
{
if ($browse.ShowDialog($topform) -eq "OK")
I think you're experiencing the issue I've faced, which is addressed in this question
The answer suggests setting .ShowHelp to $true, like this:
$openFileDialog = New-Object System.Windows.Forms.openFileDialog
$openFileDialog.ShowHelp = $true
$openFileDialog.ShowDialog() | Out-Null

Resources