備考欄に感想を書くタイプのエンジニア

それで出世が遅れ(ry

AsSecureStringの値はパスワード欄にそのまま入れると死ぬ

PowerShell 内でパスワードの情報を扱う際に -AsSecureString というパラメーターを使ったり ConvertTo-SecureString コマンドを使ったりするシーンがあるかと思います。SecureString にしない場合はこう。

PS > $Password = Read-Host -Prompt "Enter your Password..."
Enter your Password...: Password

PS > $password
Password

-AsSecureString パラメーターを付けた場合はこう。

PS > $Password = Read-Host -AsSecureString -Prompt "Enter your Password..."
#パスワード入力のためのポップアップは表示されるが、入力した内容は伏字になる
PS > $Password
System.Security.SecureString

ということで、そのままだとパスワードの値そのものは利用できないという認識だったのですが、 Windows PowerShell 4.0 for .NET Developers を見てると次のような形で Outlook.com に接続してサインインまでしてしまおうとしていて「?」となったわけです。

$EmailAddress = Read-Host -Prompt "Enter your Microsoft Account.."
$Password = Read-Host -AsSecureString -Prompt "Enter your Password..."
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible = $true
$ie.navigate("https://outlook.com")
while ($ie.Busy){Start-Sleep -Milliseconds 500}
$doc = $ie.document
$tbUsername = $doc.getElementByID("i0116")
$tbUsername.value = $EmailAddress
$tbPassword = $doc.getElementByID("i0118")
$tbPassword.value = $Password
$tbnSubmit = $doc.getElementByID("idSIButton9")
$tbnSubmit.Click()

結構回りくどい感じですが、それよりも $Password で SecureString になっているパスワードをそのまま突っ込んでいますし、実際にこのコードを実行するとサインインに失敗します。ではどうするか。1つは SecureString を平文に戻す方法です。

$EmailAddress = Read-Host -Prompt "Enter your Microsoft Account.."
$Password = Read-Host -AsSecureString -Prompt "Enter your Password..."
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible = $true
$ie.navigate("https://outlook.com")
while ($ie.Busy){Start-Sleep -Milliseconds 500}
$ie.document.getElementByID("i0116").value = $EmailAddress
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$PasswordPlain = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
$ie.document.getElementByID("i0118").value = $PasswordPlain
$ie.document.getElementByID("idSIButton9").Click()

今回は COM を使って元のパスワードに戻していますが、それでもまだ面倒くさい感じがします。もう一つの方法は Get-Credential にアカウント情報を持たせてあげる形です。というより@guitarrapc_tech 氏が下のように言っていたのを見て、そう言えば書籍に書いてあることが間違えているのに気づいたことを思い出して書いているだけです()

$cred = Get-Credential -Message "Input username for login"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible = $true
$ie.navigate("https://outlook.com")
while ($ie.Busy){Start-Sleep -Milliseconds 500}
$ie.document.getElementByID("i0116").value = $cred.UserName
$ie.document.getElementByID("i0118").value = $cred.GetNetworkCredential().Password
$ie.document.getElementByID("idSIButton9").Click()

こちらのほうが比較的シンプルな感じでしょうか。。。他にいい方法があれば教えてくだしあ。 ※それでもやはりパスワードの情報を利用する際には GetNetworkCredential メソッドを使わないと正しくパスワード情報を得られなくて死にます。

coexistence-configuration の PSsnap-in を追加すると何ができるようになるのか

PowerShell から手動で Office 365 とディレクトリ同期を行うときに Add-PSsnapin coexistence-configuration を実行してから Start-OnlineCoexistenceSync を実行するというのは検索すればすぐに出てくるので新しい話ではないのですが、ちょっと待て、と。よく考えてみると Start-OnlineCoexistenceSync だけを実行するために PSsnap-in を落としてくるのはちょっと非効率すぎないかと思って、中身を見てみました。

Get-Command -Module Coexistence-Configuration | ft CommandType,Name -AutoSize 


CommandType Name                                
----------- ----                             
Cmdlet      Disable-DirSyncLog                    
Cmdlet      Disable-MSOnlinePasswordSync      
Cmdlet      Disable-MSOnlineRichCoexistence    
Cmdlet      Disable-PasswordSyncLog       
Cmdlet      Enable-DirSyncLog          
Cmdlet      Enable-MSOnlinePasswordSync  
Cmdlet      Enable-MSOnlineRichCoexistence   
Cmdlet      Enable-PasswordSyncLog    
Cmdlet      Get-CoexistenceConfiguration     
Cmdlet      Get-DirSyncLogStatus    
Cmdlet      Get-PasswordSyncLogStatus   
Cmdlet      Set-CoexistenceConfiguration   
Cmdlet      Set-CompanyDirSyncFeatures  
Cmdlet      Set-FullPasswordSync                  
Cmdlet      Start-OnlineCoexistenceSync           
Cmdlet      Update-MSOLDirSyncNetworkProxySetting

他にもたくさんありますねw 上で列挙されたコマンドの名前を見るとだいたい何をするためのコマンドかわかりますが、coexistence-configuration スナップインを追加することで Windows Azure Active Directory ディレクトリ同期ツールの構成ウィザードで設定する内容を PowerShell で実行するための(本当はツールが裏で PS を実行しているだけだと思いますが)コマンド各種が実行可能になるようです。詳細は後述で Get-Help -Detailed した内容を貼付けておくので興味があれば見てみてください(ところでこれらのコマンドがなぜ公式で一切情報提供されてないんでしょうか…)。


Enable-MSOnlineRichCoexistence って何かと思ったのですがおそらく構成ウィザードにおける [混合環境を有効にする] で、Azure -> On-Premise の方向での同期の権限付与とかそういう動作のためのコマンドみたいです。

こんな KB 出てました→ Attributes for Exchange Online aren't written back to the on-premises Active Directory directory service in an Exchange hybrid deployment

あと、Update-MSOLDirSyncNetworkProxySettingError message when you run the Directory Sync tool Configuration Wizard: "Your credentials could not be authenticated" という KB が出ていることから察するに、構成ウィザードでは WinHTTP のプロキシの設定を拾ってくれる一方 PowerShell の場合は明示的にこのコマンドでプロキシの設定を拾わないとだめな感じでしょうか。

名前
    Disable-DirSyncLog
   
概要
    This commandlet is used to disable logging for the Azure Active Directory Sync tool.
   
構文
    Disable-DirSyncLog [<CommonParameters>]
   
説明
    This commandlet is used to disable logging for the Azure Active Directory Sync tool.
   
パラメーター
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Example  --------------
   
    C:\PS>Disable-DirSyncLog
   
注釈
    例を参照するには、次のように入力してください: "get-help Disable-DirSyncLog -examples".
    詳細を参照するには、次のように入力してください: "get-help Disable-DirSyncLog -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Disable-DirSyncLog -full".
 
 
 
名前
    Disable-MSOnlinePasswordSync
   
構文
    Disable-MSOnlinePasswordSync -Credential <pscredential> [-WhatIf] [-Confirm]  [<CommonParameters>]
   
パラメーター
    -Confirm
   
    -Credential <pscredential>
   
    -WhatIf
   
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
エイリアス
    なし
   
注釈
    なし
 

 
名前
    Disable-MSOnlineRichCoexistence
   
構文
    Disable-MSOnlineRichCoexistence -Credential <pscredential> [-WhatIf] [-Confirm]  [<CommonParameters>]
   
パラメーター
    -Confirm
   
    -Credential <pscredential>
   
    -WhatIf
   
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
エイリアス
    なし
   
注釈
    なし
 
 
 
名前
    Disable-PasswordSyncLog
   
概要
    This commandlet is used to disable logging for the Password Sync feature of the Azure Active Directory Sync tool.
       
構文
    Disable-PasswordSyncLog [<CommonParameters>]
       
説明
    This commandlet is used to disable logging for the Password Sync feature of the Azure Active Directory Sync tool.
 
パラメーター
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Example  --------------
   
    C:\PS>Disable-PasswordSyncLog
   
注釈
    例を参照するには、次のように入力してください: "get-help Disable-PasswordSyncLog -examples".
    詳細を参照するには、次のように入力してください: "get-help Disable-PasswordSyncLog -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Disable-PasswordSyncLog -full".
 
 
 
名前
    Enable-DirSyncLog
   
概要
    This commandlet is used to configure the logging level for the Azure Active Directory Sync tool.
    
構文
    Enable-DirSyncLog [-FilePath <String>] [-TraceLevel <String>] [<CommonParameters>]
      
説明
    This commandlet is used to configure the logging level for the Azure Active Directory Sync tool.
 
パラメーター
    -FilePath <String>
        Specifies the location in the files system where the log file should be written to.  If not speci
        fied, defaults to the location where the Directory Sync tool is installed.
       
    -TraceLevel <String>
        Specifies the level to configure the Directory Sync tool to log at.  Valid options are: “Error”,
        “Warning”, “Info”, “Verbose”. If not specified, defaults to “Verbose”.
        
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Example  --------------
   
    C:\PS>Enable-DirSyncLog -TraceLevel Info -FilePath C:\DirSync.log
    
注釈
    例を参照するには、次のように入力してください: "get-help Enable-DirSyncLog -examples".
    詳細を参照するには、次のように入力してください: "get-help Enable-DirSyncLog -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Enable-DirSyncLog -full".
 
 
 
 
名前
    Enable-MSOnlinePasswordSync
   
構文
    Enable-MSOnlinePasswordSync -Credential <pscredential> [-WhatIf] [-Confirm]  [<CommonParameters>]
    
パラメーター
    -Confirm
   
    -Credential <pscredential>
   
    -WhatIf
   
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
 エイリアス
    なし

注釈
    なし
 
 
 
 
名前
    Enable-MSOnlineRichCoexistence
   
構文
    Enable-MSOnlineRichCoexistence -Credential <pscredential> [-WhatIf] [-Confirm]  [<CommonParameters>]
   
パラメーター
    -Confirm
   
    -Credential <pscredential>
   
    -WhatIf
   
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
エイリアス
    なし
   
注釈
    なし
 
 
 
 
名前
    Enable-PasswordSyncLog
   
概要
    This commandlet is used to configure the logging level for the Password Sync feature of the Azure Active Directory Sync tool.
   
構文
    Enable-PasswordSyncLog [-FilePath <String>] [-TraceLevel <String>] [<CommonParameters>]
   
説明
    This commandlet is used to configure the logging level for the Password Sync feature of the Azure Active Directory Sync tool.
 
パラメーター
    -FilePath <String>
        Specifies the location in the files system where the log file should be written to.  If not speci
        fied, defaults to the location where the Directory Sync tool is installed.
       
    -TraceLevel <String>
        Specifies the level to configure the Directory Sync tool to log at.  Valid options are: “Error”, “Warning”, “Info”, “Verbose”.  If not specified, defaults to 'Verbose'.
       
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Example  --------------
   
    C:\PS>Enable-PasswordSyncLog -TraceLevel Info -FilePath C:\PasswordSync.log
   
注釈
    例を参照するには、次のように入力してください: "get-help Enable-PasswordSyncLog -examples".
    詳細を参照するには、次のように入力してください: "get-help Enable-PasswordSyncLog -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Enable-PasswordSyncLog -full".
 
 
 
 
名前
    Get-CoexistenceConfiguration
   
概要
    Gets a configuration information from the Microsoft Online Coexistence Web Server   
    
構文
    Get-CoexistenceConfiguration -TargetCredentials <PSCredential> [<CommonParameters>]
    
説明
    Gets a configuration information from the Microsoft Online Coexistence Web Server
 
パラメーター
    -TargetCredentials <PSCredential>
        Administrative credentials for a company in Microsoft Online.
       
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Get Configuration --------------
   
    C:\PS>$cnfg = Get-CoexistenceConfiguration
   
    Gets a configuration object from the Microsoft Online Web Service    
    
注釈
    例を参照するには、次のように入力してください: "get-help Get-CoexistenceConfiguration -examples".
    詳細を参照するには、次のように入力してください: "get-help Get-CoexistenceConfiguration -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Get-CoexistenceConfiguration -full".
 
 
 
 
名前
    Get-DirSyncLogStatus
   
概要
    This commandlet is used to retrieve the current logging level for the Azure Active Directory Sync tool.
    
構文
    Get-DirSyncLogStatus [<CommonParameters>]
    
説明
    This commandlet is used to retrieve the current logging level for the Azure Active Directory Sync tool.  
 
パラメーター
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Example  --------------
    
    C:\PS>Get-DirSyncLogStatus
      
注釈
    例を参照するには、次のように入力してください: "get-help Get-DirSyncLogStatus -examples".
    詳細を参照するには、次のように入力してください: "get-help Get-DirSyncLogStatus -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Get-DirSyncLogStatus -full".
 
 
 
 
名前
    Get-PasswordSyncLogStatus
   
概要
    This commandlet is used to retrieve the current logging level for the Password Sync feature of the Azure Active Directory Sync tool.
    
構文
    Get-PasswordSyncLogStatus [<CommonParameters>]
   
説明
    This commandlet is used to retrieve the current logging level for the Password Sync feature of the Azure Active Directory Sync tool.
 
パラメーター
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Example  --------------
   
    C:\PS>Get-PasswordSyncLogStatus
   
注釈
    例を参照するには、次のように入力してください: "get-help Get-PasswordSyncLogStatus -examples".
    詳細を参照するには、次のように入力してください: "get-help Get-PasswordSyncLogStatus -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Get-PasswordSyncLogStatus -full".
 
 
 
 
名前
    Set-CoexistenceConfiguration
   
概要
    Configures Microsoft Online Directory Synchronization Tool.
 
構文
    Set-CoexistenceConfiguration -SourceCredentials <PSCredential> -TargetCredentials <PSCredential> [-Config] <CoexistenceConfiguration> [-Path <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
     
説明
    Configures Microsoft Online Directory Synchronization Tool.
 
パラメーター
    -SourceCredentials <PSCredential>
        Enterprise Admin credentials for the forest that will be sent up to Microsoft Online.  These credentials are used to create an account in Active Directory that has the minimum required permissions to read from Active Directory.
       
    -TargetCredentials <PSCredential>
        Administrative credentials for a company in Microsoft Online.
       
    -Config <CoexistenceConfiguration>
        Configuration object returned from Get-CoexistenceConfiguration
       
    -Path <String>
        The directory which contains XML that defines the default management agents and the metaverse schema.
       
    -WhatIf
       
        
    -Confirm
       
        
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Configuration --------------
   
    C:\PS>$cnfg = Get-CoexistenceConfiguration
    Set-CoexistenceConfiguration $cnfg
   
    Configures Microsoft Online Directory Synchronization.
   
 注釈
    例を参照するには、次のように入力してください: "get-help Set-CoexistenceConfiguration -examples".
    詳細を参照するには、次のように入力してください: "get-help Set-CoexistenceConfiguration -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Set-CoexistenceConfiguration -full".
 
 
 
 
名前
    Set-CompanyDirSyncFeatures
   
構文
    Set-CompanyDirSyncFeatures -TargetCredentials <pscredential> -FeaturesFlag <int>  [<CommonParameters>
    ]
    
パラメーター
    -FeaturesFlag <int>
   
    -TargetCredentials <pscredential>
   
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
 
エイリアス
    なし
  
注釈
    なし
 
 
 
 
名前
    Set-FullPasswordSync
   
概要
    Resets the password sync state information forcing a full sync the next time the service is restarted.  
    
構文
    Set-FullPasswordSync [<CommonParameters>]
    
説明
    Resets the password sync state information forcing a full sync the next time the service is restarted.   
 
パラメーター
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Set FullPasswordSync --------------
   
    C:\PS>Set-FullPasswordSync
   
    Resets the password sync state information forcing a full sync the next time the service is restarted.   
    
注釈
    例を参照するには、次のように入力してください: "get-help Set-FullPasswordSync -examples".
    詳細を参照するには、次のように入力してください: "get-help Set-FullPasswordSync -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Set-FullPasswordSync -full".
 
 
 
 
名前
    Start-OnlineCoexistenceSync
   
概要
    Starts synchronization with Microsoft Online
   
構文
    Start-OnlineCoexistenceSync [-FullSync] [-WhatIf] [-Confirm] [<CommonParameters>]
    
説明
    Starts synchronization with Microsoft Online
   
パラメーター
    -FullSync
        Performs a full sync with Microsoft Online.
       
    -WhatIf
       
        
    -Confirm
       
        
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  Start Sync --------------
   
    C:\PS>Start-OnlineCoexistenceSync
   
    Starts a directory sync with Microsoft Online.
   
    
    
    -----------  Start Full Sync ------------
   
    C:\PS>Start-OnlineCoexistenceSync -FullSync
   
    Performs a full sync with Microsoft Online.
   
注釈
    例を参照するには、次のように入力してください: "get-help Start-OnlineCoexistenceSync -examples".
    詳細を参照するには、次のように入力してください: "get-help Start-OnlineCoexistenceSync -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Start-OnlineCoexistenceSync -full".
 
 
 
 
名前
    Update-MSOLDirSyncNetworkProxySetting
   
概要
    Updates the directory sync service to use the current user's http proxy settings.
   
構文
    Update-MSOLDirSyncNetworkProxySetting [[-NoProxyUpdate]] [-WhatIf] [-Confirm] [<CommonParameters>]
    
説明
    Copies the current user's http proxy settings off to a .config file.  The directory sync will apply these settings each time it communicates with the directory sync web service
   
パラメーター
    -NoProxyUpdate
        Causes directory sync to ignore the http proxy settings that were saved in a .config file.
       
    -WhatIf
       
        
    -Confirm
       
        
    <CommonParameters>
        このコマンドレットは、次の共通パラメーターをサポートします: Verbose、
        Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、および OutVariable。詳細については、about_CommonParameters
        (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。
   
    --------------  EXAMPLE 1 --------------
   
    C:\PS>Update-MSOLDirSyncNetworkProxySetting
   
    Updates the http proxy settings that directory sync will use
   
    
    
    
    --------------  EXAMPLE 2 --------------
   
    C:\PS>Update-MSOLDirSyncNetworkProxySetting -NoProxyUpdate
   
    Disables the automatic application of current user setting to the directory sync service.
   
    
注釈
    例を参照するには、次のように入力してください: "get-help Update-MSOLDirSyncNetworkProxySetting -examples".
    詳細を参照するには、次のように入力してください: "get-help Update-MSOLDirSyncNetworkProxySetting -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Update-MSOLDirSyncNetworkProxySetting -full".
    オンライン ヘルプを参照するには、次のように入力してください: "get-help Update-MSOLDirSyncNetworkProxySetting -online"

PowerShell によるパスワードの生成に char 型を使ってみる

ちょっと Active Directory でユーザーを自動的に作成する際に予めランダムかつある程度の複雑性があるパスワードを設定しておきたいなということで、これを生成する方法が必要になりました。それで、私はゆとり PowerShell-er なのでまず検索してみますと powershell.com にこんなスクリプトがありました。

    function Get-RandomPassword {
        param(
            $length = 10,
            $characters = 'abcdefghkmnprstuvwxyzABCDEFGHKLMNPRSTUVWXYZ123456789!"§\$%&/()=?*+#_'
            )
    
        $random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
         # output random pwd
         $private:ofs=""
         [String]$characters[$random]
    } 

文字列の生成方法としては、予め候補となる文字記号数字を $characters に格納し、$length で文字数を指定して Get-Randam でそれらの情報を利用して文字列をランダムに生成している感じで特別変わったことはしてない感じです。不足があれば記号とか増やしてあげるといいです。

次はこちら。これは Windows Server 2012 Automation with PowerShell Cookbook の Chapter 2, Creating AD Users に書かれていたスクリプトです。上述のスクリプトに対してこちらのスクリプトの書き方がちょっとおもしろいというか、理屈を考えれば何ら不思議のない書き方なのですが、あんまり見たことがない書き方かなということでこちらが本題(?)です。

function Get-RandomPass{
    $newPass = ''
    1..10 | ForEach-Object {
    $newPass += [char](Get-Random -Minimum 48 -Maximum 122)
    }
    return $newPass
}

$newPass の部分を見ると [char](Get-random -Minimun 48 -Maximum 122) という部分があります。ここで何をしているかというと Get-Random-Minimum および -Maximum パラメータで指定した範囲内でランダムに取得された int 型の値を10進法の ASCII コードとして扱うべく [char] で char 型に変換した結果として文字を取得しています。

上のスクリプトで指定している最小値および最大値から判断する限り、以下の文字の中からランダムに文字列を生成することが意図されていると考えられますが、この理屈から言うと ASCII 33 (min) - 126 (max) までが範囲として最大となり、最も多くの記号を利用することになります1。ASCII 32 まで含むとスペースを含むことになりますね。

$characters = 
'0123456789:;<=>?@ABCDEFHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz'

どちらの方法がどうだとかいうことではありませんが、fizzbuzz にもいろいろな書き方があるような感じで「へぇ」ぐらいに受け取ってもらえれば。

Get-HofixのInstalledOnの時間が取得できない(未解決)

タイトルの通りなのですが…。

PS >Get-HotFix | Format-Table -AutoSize

Source    Description HotFixID  InstalledBy         InstalledOn         
------    ----------- --------  -----------         -----------         
TESTLAB01 Update      KB2871777 NT AUTHORITY\SYSTEM 3/1/2014 12:00:00 AM
TESTLAB01 Update      KB2877213 NT AUTHORITY\SYSTEM 3/1/2014 12:00:00 AM
TESTLAB01 Update      KB2891804 NT AUTHORITY\SYSTEM 3/1/2014 12:00:00 AM
TESTLAB01 Update      KB2904266 NT AUTHORITY\SYSTEM 3/1/2014 12:00:00 AM

Get-Hotfix でインストールした Hotfix の情報を取得すると InstalledOn のパラメータの時刻の値が勝手に 12:00:00 AM になってしまいます。それで調べてみたところ同じような現象が発生している方がいらっしゃったようでフォーラムに書き込みがされているのをいくつか見ていたところ、Microsoft Connect で報告されている以下のバグのような気がしますが、まだ fix されていないという認識でいいのでしょうかね…。

Get-Hotfix show date only if date parsing has same rules as in en-US Culture.

work around として一応 $env:windir\WindowsUpdate.log の文字列を Select-String して Hotfix の情報を取る方法とかあるみたいですが微妙な感じです…。

8 Faces Issue #7 と Helvetica

もう昨年末の話になるのですが、11月頃?に発行された 8 Faces Issue #7 で最初にインタビュー記事が掲載されている Michael Bierut 氏の Helvetica に関する発言の中で、最後に氏が選んだ8つのタイプフェイスのうちの1つ、Helvetica の部分に書かれているコメントが以下の通りとなるのですが、少しおもしろいなと思うところがありまして。

Because I'm in the documentary about it, people tend to associate me with Helvetica. YET I'VE ALWAYS TRIED TO RESIST IT, WHICH IS DIFFICULT. Like others, I've tried to improve it, but that's difficult too. For our wayfinding for the New York City Department of Transportation, we changed all the square dots to round, which I enjoyed. (p.16)

Michael Bierut 氏と Helvetica と言えば、まさに Helvetica というタイトルのドキュメンタリー映画に出演し非常に印象的な以下の発言をしていましたが、このドキュメンタリー映画には氏以外にも様々なタイプデザイナーの方が出演されており、それぞれが各々の観点で Helvetica について話していたことがこのドキュメンタリー映画の大きな特徴と言えると思います(今更な話ではありますが)。

一方で "people tend to associate me with Helvetica." というようなことを他のタイプデザイナーが言っているを聞いた/見たことは寡聞にしてありません(どなたかそういう発言をされているソースとかお持ちでしょうか)。

"Can you imagine how bracing and thrilling that was? That must have felt like you had crawled through a desert with your mouth caked with filthy dust, and then someone offers you a clear, refreshing distilled icy glass of water … it must have just been fantastic."


Helvetica - PERIOD. - YouTube

Issue #7 が発行された 2013 年当時 Helvetica に関して話題になっていたのは iOS 7 が Helvetica Neue Ultra Light を採用したとかそういうことぐらいで、今更と言っては語弊がありますが Helvetica について現在進行形で Helvetica があふれる世の中に抵抗してみたとか Helvetica をよりよくしようとしたというようなことを語るタイプデザイナーをあまり見かけない気がして、逆に新鮮な感じでした。そういう所がおもしろかったよ、という話です。

もちろん iOS 7 で Helvetica Neue Ultra Light を採用するのは適切ではなかったというような話はありましたが、それはタイプフェイスそのものの話ではないので…。

あと、どうでもいいことなのですが Helvetica の特徴について、おそらく Wikipedia からの引用だと思われるのですが”簡素な”という形容詞を使われる方をたまに見かけます。しかしながら Helvetica を表すのに簡素というのはあまり適切ではないように思います。感覚の問題と言ってしまえばそうかもしれませんが、いったいどの辺りが簡素なのか、じゃあ例えば Frutiger はどうなのか。。。

Azure ストレージ アカウントを作成するときにちょっとハマった

Windows Azure で Azure ストレージを利用する場合、ストレージ アカウントを作成する必要がありますが、Windows Azure PowerShell も公開されていることですし、Web 上のポータル サイトで GUI をポチポチせずとも常に起動しっぱなしの PowerShell でサッと作ってしまおうと思うわけです。こんな風に。

PS > New-AzureStorageAccount -StorageAccountName "azureStorage1" -Label "Storage Account for SQL" -Location "North Central US"

すると、こんなエラーが出ます。

 New-AzureStorageAccount : 指定された引数は、有効な値の範囲内にありません。
 パラメーター名: parameters.ServiceName
 発生場所 行:1 文字:1
 + New-AzureStorageAccount `
 + ~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : NotSpecified: (:) [New-AzureStorageAccount], ArgumentOutOfRangeException
     + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.WindowsAzure.Commands.ServiceManagement.Storage 
    Services.NewAzureStorageAccountCommand

最初は有効な値の範囲内とか何事かと思ったのですが、ちゃんと New-AzureStorageAccount に関する公式サイトWindows Azure の Web 上の GUI、そして PowerShellGet-Help New-AzureStorageAccount -Detailed には書いてあります(-Detailed パラメーターがない場合はフィールドの文字入力制限について言及されている文章は出てきません)。

フィールドは 3 ~ 24 文字で指定する必要があります。 フィールドに使用できるのは、小文字のアルファベットおよび数字のみです。 (The storage account name (...) must be between 3 and 24 characters in length and use lowercase letters and numbers only. )

なぜこういう制限があるかというとおそらく、サービスのエンドポイントの URL の形式が以下のようになっていて、大文字と記号が使えないからだと思われます。

http://mystorageaccount.*.core.windows.net/*

ともかく、いくら PowerShell が "動詞-名詞" 形式の命名規則で、ストレージ アカウントを新たに作成するなら NewStorageAccount な感じかなーあーあったあったーってヘラヘラ使ってると、ときどきこんな落とし穴に…(普通ははまらないですね)。

第1回 PowerShell 勉強会に参加してきた

12月21日に開催された第1回 PowerShell 勉強会に参加してきたのですが、内容は後述のリンクを見てもらうとして、雑感というか。

個人的に PowerShell は登場してから結構年月が経っているにもかかわらず利用者がそれほど多くないという印象を持っていて、だからというわけではないですが PowerShell というカテゴリでの勉強会に魅力を感じて参加してみた結果、想像以上に面白い勉強会だったと思っています。

また、登壇者をはじめとして PowerShell を様々な形で利用している(利用できる)のは公開されているスライドを見れば明らかで、現時点でも PowerShell は非常に強力な言語で今後さらによくなっていくものと思われます(vbs とは対照的に)。

一方で、勉強会の中でWindows PowerShell イン アクションは聖書みたいな話があって、それに異論はないのですが in Action の Second Edition も in Depth も Deep Dive も出版されているにもかかわらず翻訳されていない、v3 に対応している日本語の書籍が @mutaguchi 氏著の【改訂新版】 Windows PowerShell ポケットリファレンスだけ(?)みたいな状況を見るに Windows 系エンジニアは基本的に英語の書籍が読めて翻訳書籍の需要がないか、PowerShell に興味がないかのどっちかで、どちらかというと後者っぽい雰囲気を"ソースは俺"レベルの観測範囲で感じますが実際のところどうなんでしょうか。

懇親会の時にも、PowerShell について日本語でぐぐると結構な確率で @mutaguchi 氏@guitarrapc_tech 氏の記事が出てくるよねっていう話をちょっとしていたのですが、PowerShell Advent Calendar レベルでほかの人ももっとエントリーを書いて PowerShell のググラビリティをあげていくと、例えばサーバー構成においてスクリーン ショットとセットになったエクセル手順書は必須みたいな世界にさよならできる、みたいないいことがあったりするのかなと PowerShell 推進派としては思ったのでした(最初からそんな世界自体が藁人形だったらいいのですが)。

ところで会場の株式会社グラニにある UFO キャッチャーを試す権利を得た人の中で一人だけマスコットをゲットできなかったのがこちらのあかうんt(ry