Skip to content

Instantly share code, notes, and snippets.

#Execute as .ps1
$text = @'
a
b
c
'@
Write-Host $text.Length
$ErrorActionPreference = "Stop"

function Main
{
    try
    {
        $logFilePath = 'C:\NotFound.log'
        if (Test-Path $logFilePath)
        {
$templates = @'
権限 :{0}
グループ名: {1}
含まれているユーザー:
{2}
'@
Add-Type -AssemblyName System.DirectoryServices
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
#Need to execute in Visual Studio command prompt context
#ClientCertificate EKU
makecert.exe -r -pe -a sha256 -n "CN=Azure Management Certificate" -ss My -len 2048 -sy 24 -e "01/01/2040" -eku 1.3.6.1.5.5.7.3.2 AzureManagementCertificate.cer >$null
$cert = Get-Childitem Cert:\CurrentUser\My | where Subject -like "*Azure Management Certificate*"
makecert.exe -r -pe -a sha256 -n "CN=*.cloudapp.net" -sky exchange -len 2048 -e "01/01/2040" -cy end -eku "1.3.6.1.5.5.7.3.1" -sv temp.pvk cloudapp.net.cer > $null
$ErrorActionPreference = 'Stop'
function Main
{
Use-NuGetPackage -PackageId Microsoft.IdentityModel.Clients.ActiveDirectory -Version 1.0.3 -Verbose #Require PSNuGet <https://github.com/altrive/PSNuGet>
#Azure AD native client application settings
$authority = 'https://login.windows.net/{0}.onmicrosoft.com' -f 'altrive' #Set AzureAD tenant
$clientId = '[Native Client Application ClientID]' #Native Client Application ClientID
$redirectUri = [Uri] 'http://localhost' #Dummy URL
param (
$RequestUrl = "http://127.0.0.1"
)
function Main
{
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.Net.Http
#Get ServicedPoint
function Get-LocalVariable
{
[CmdletBinding()]
param (
)
#define buildin variables
$builtinVariable = [PSObject].Assembly.GetType('System.Management.Automation.SpecialVariables').GetFields('NonPublic,Static') | where FieldType -eq ([string]) | foreach GetValue($null)
$builtinVariable += @("FormatEnumerationLimit", "MaximumAliasCount", "MaximumDriveCount", "MaximumErrorCount", "MaximumFunctionCount", "MaximumHistoryCount", "MaximumVariableCount", "profile", "psISE", "PSSessionOption", "PSUICulture", "psUnsupportedConsoleApplications", "PSVersionTable", "startupFiles")
Use-NuGetPackage Microsoft.OData.Client -Verbose #Use PSNuGet <https://github.com/altrive/PSNuGet>
$context = New-Object Microsoft.OData.Client.DataServiceContext([uri] "https://graph.windows.net")
<#
New-Object : "1" 個の引数を指定して ".ctor" を呼び出し中に例外が発生しました: "'ClientEdmModelCache' のタイプ初期化子が例外をスローしました。"
System.IO.FileNotFoundException: ファイルまたはアセンブリ 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7ce
c85d7bea7798e, Retargetable=Yes'、またはその依存関係の 1 つが読み込めませんでした。指定されたファイルが見つかりません。
ファイル名 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' です。'Sys
tem.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'

Add CredentialManager support

It's seems many people creating their own credential management solution.

It's seems, solutions classified 2 types of approach. and there are pros/cons respectively.

  1. Use OS native Windows Credential Manager(by P/Invoke).
  2. Persist encrypted credential to file(JSON/XML) under $profile directory.

Please add standard credential management support to PowerShell.

#Load NuGet.Core.dll assembly(Load dll as byte stream to avoid dll locking issue)
$loadedDll = [AppDomain]::CurrentDomain.GetAssemblies() | where { $_.FullName.StartsWith("NuGet.Core")}
if($null -eq $loadedDll)
{
$dllPath = Join-Path $PSScriptRoot "NuGet.Core.dll" -Resolve
$dllBytes = [IO.File]::ReadAllBytes($dllPath)
[System.Reflection.Assembly]::Load($dllBytes) > $null
}