溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Powershell DSC 5.0 - 資源的使用

發(fā)布時間:2020-07-20 03:52:03 來源:網(wǎng)絡(luò) 閱讀:1165 作者:beanxyz 欄目:大數(shù)據(jù)

Powershell DSC 自帶了一些常見的資源。如果需要新的,可以下載安裝 DSC Resource Kit 10或者從Powershell Gallery下載。


比如說查看當(dāng)前已經(jīng)安裝了的資源

PS C:\Windows\system32> Get-DscResource
ImplementedAs   Name                      ModuleName                     Version    Properties                                        
-------------   ----                      ----------                     -------    ----------                                        
Binary          File                                                                {DestinationPath, Attributes, Checksum, Content...
PowerShell      Archive                   PSDesiredStateConfiguration    1.1        {Destination, Path, Checksum, Credential...}      
PowerShell      Environment               PSDesiredStateConfiguration    1.1        {Name, DependsOn, Ensure, Path...}                
PowerShell      Group                     PSDesiredStateConfiguration    1.1        {GroupName, Credential, DependsOn, Description...}
Binary          Log                       PSDesiredStateConfiguration    1.1        {Message, DependsOn, PsDscRunAsCredential}        
PowerShell      Package                   PSDesiredStateConfiguration    1.1        {Name, Path, ProductId, Arguments...}             
PowerShell      Registry                  PSDesiredStateConfiguration    1.1        {Key, ValueName, DependsOn, Ensure...}            
PowerShell      Script                    PSDesiredStateConfiguration    1.1        {GetScript, SetScript, TestScript, Credential...} 
PowerShell      Service                   PSDesiredStateConfiguration    1.1        {Name, BuiltInAccount, Credential, Dependencies...
PowerShell      User                      PSDesiredStateConfiguration    1.1        {UserName, DependsOn, Description, Disabled...}   
PowerShell      WaitForAll                PSDesiredStateConfiguration    1.1        {NodeName, ResourceName, DependsOn, PsDscRunAsC...
PowerShell      WaitForAny                PSDesiredStateConfiguration    1.1        {NodeName, ResourceName, DependsOn, PsDscRunAsC...
PowerShell      WaitForSome               PSDesiredStateConfiguration    1.1        {NodeCount, NodeName, ResourceName, DependsOn...} 
PowerShell      WindowsFeature            PSDesiredStateConfiguration    1.1        {Name, Credential, DependsOn, Ensure...}          
PowerShell      WindowsOptionalFeature    PSDesiredStateConfiguration    1.1        {Name, DependsOn, Ensure, LogLevel...}            
PowerShell      WindowsProcess            PSDesiredStateConfiguration    1.1        {Arguments, Path, Credential, DependsOn...}       
PowerShell      xArchive                  xPSDesiredStateConfiguration   3.5.0.0    {Destination, Path, CompressionLevel, DependsOn...
PowerShell      xDSCWebService            xPSDesiredStateConfiguration   3.5.0.0    {CertificateThumbPrint, EndpointName, AcceptSel...
PowerShell      xGroup                    xPSDesiredStateConfiguration   3.5.0.0    {GroupName, Credential, DependsOn, Description...}
PowerShell      xPackage                  xPSDesiredStateConfiguration   3.5.0.0    {Name, Path, ProductId, Arguments...}             
PowerShell      xPSEndpoint               xPSDesiredStateConfiguration   3.5.0.0    {Name, AccessMode, DependsOn, Ensure...}          
PowerShell      xRemoteFile               xPSDesiredStateConfiguration   3.5.0.0    {DestinationPath, Uri, Credential, DependsOn...}  
PowerShell      xService                  xPSDesiredStateConfiguration   3.5.0.0    {Name, BuiltInAccount, Credential, Dependencies...
PowerShell      xWindowsOptionalFeature   xPSDesiredStateConfiguration   3.5.0.0    {Name, DependsOn, Ensure, LogLevel...}            
PowerShell      xWindowsProcess           xPSDesiredStateConfiguration   3.5.0.0    {Arguments, Path, Credential, DependsOn...}


下面是一些常見的例子:


  1. 拷貝文件 資源叫做 File


configuration TestFile {
    Node sydittest {
        File ZipFile {
            Ensure = "Present" 
            Type = "Directory“ # Default is “File”
            Force = $True
            Recurse = $True
            SourcePath = '\\sydit01\test'
            DestinationPath = 'C:\Downloads'  # On Sydittest
        }
    }
}
TestFile -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force


Powershell DSC 5.0 - 資源的使用

查看該zip文件已經(jīng)拷貝了

Powershell DSC 5.0 - 資源的使用


2.解壓zip文件,資源叫做 Archive


configuration TestArchive {
    Node sydittest {
        Archive Unzip{
            Destination = 'C:\unzip'
            Path = 'c:\downloads\temp.zip'
            Checksum = 'SHA-256'
            Validate = $true
            Force = $true
            Ensure = 'Present'
        }
    }
}
TestArchive -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -Force


Powershell DSC 5.0 - 資源的使用


該文件已經(jīng)解壓到指定目錄

Powershell DSC 5.0 - 資源的使用


3. 創(chuàng)建環(huán)境變量 資源叫做Environment


configuration TestEnvVar {
    Node sydittest {
        Environment NewVar{
            Name = 'MYNEWVAR'
            Value = 'Value to store'
            Ensure = 'Present'
        }
    }
}
TestEnvVar -OutputPath c:\DSC\Mod5Config


Powershell DSC 5.0 - 資源的使用


確認(rèn)該環(huán)境變量已經(jīng)創(chuàng)建

Powershell DSC 5.0 - 資源的使用


4.創(chuàng)建本地用戶 資源是User

這個是幾個資源里面相對麻煩的一個,因?yàn)槟J(rèn)傳遞的是明文,他不會允許你推送的,我需要明確告訴他允許明文。


configuration TestUser
{
    param
    ( 
        [PSCredential]$Credential
    )
    node sydittest
    {    
        User TestUser
        {
            UserName = $Credential.UserName
            Ensure = 'Present'
            Password = $Credential
            Description = 'User created by DSC'
            PasswordNeverExpires = $true
            PasswordChangeNotAllowed = $true
        }
    }
}
$ConfigData = @{   
    AllNodes = @(        
        @{     
            NodeName = 'sydittest'
            PSDscAllowPlainTextPassword=$true
        } 
    )  
} 
TestUser -ConfigurationData $ConfigData -Credential (Get-Credential) -OutputPath c:\DSC\Mod5Config


可以看見是明文發(fā)送的,如果是生產(chǎn)環(huán)境,需要用證書加密的才行。


Powershell DSC 5.0 - 資源的使用


推送出去

Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force

Powershell DSC 5.0 - 資源的使用


可以看見用戶已經(jīng)創(chuàng)建了

Powershell DSC 5.0 - 資源的使用


5.創(chuàng)建本地組 資源 Group


configuration TestGroup {
    Node sydittest {
        Group CreateGroup {
            Ensure = 'Present'
            GroupName = 'TestGroup'
            Description = 'This is a DSC test group'
            Members = 'administrator'
        } 
        
    }
}
TestGroup -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force

Powershell DSC 5.0 - 資源的使用


6.創(chuàng)建注冊表鍵 資源 Registry


configuration TestRegistry {
    Node sydittest {
        Registry CreateReg {
            Key = 'HKEY_Local_Machine\Software\DSCTest'
            ValueName = 'DSCTestGood'
            ValueType = 'string'
            ValueData = 'True'
        } 
    }
}
Testregistry -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force


Powershell DSC 5.0 - 資源的使用


確認(rèn)成功創(chuàng)建

Powershell DSC 5.0 - 資源的使用


7. 創(chuàng)建進(jìn)程(運(yùn)行某個程序) 資源是 Windowsprocess

configuration TestProcess {
    Node sydittest {
        WindowsProcess CreateNotepad {
            Path = 'notepad.exe'
            Arguments = ''
        } 
        WindowsProcess CreatePaint {
            Path = 'mspaint.exe'
            Arguments = ''
        }
    }
}
TestProcess -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force


確認(rèn)

Powershell DSC 5.0 - 資源的使用


8. 更改服務(wù)的狀態(tài)

configuration TestService {
    Node sydittest {
        Service StartAudio {
            Name = 'Audiosrv'
            State = 'Running'
            StartupType = 'Automatic'
        } 
    }
}
TestService -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force


更改前

Powershell DSC 5.0 - 資源的使用


更改

Powershell DSC 5.0 - 資源的使用

更改后

Powershell DSC 5.0 - 資源的使用


9. 腳本資源


這個是當(dāng)現(xiàn)有的資源都無法滿足需求的時候,可以進(jìn)行自定義資源。里面有3個子模塊,test,set,get


首先通過 test 判斷狀態(tài),返回一個boolean值,如果是true,忽略;如果是false,那么在set里面進(jìn)行處理;相當(dāng)于于一個if else的判斷語句。get可以通過getdsc的命令獲取當(dāng)前狀態(tài)


下面的例子是判斷如果bits服務(wù)開啟,那么關(guān)閉

configuration TestScript {
    Node sydittest {
        Script TestScript {
            GetScript = {
                @{
                    GetScript = $GetScript
                    SetScript = $setScript
                    TestScript = $TestScript
                    Result = (Get-Service -name bits).status
                }           
            }
            SetScript = {
                stop-Service -name bits
            }
            TestScript = {
            
                $Status=(Get-service -name bits).status
                $Status -eq 'stopped'
            }
        }
        
    }
}
Testscript -OutputPath c:\DSC\Mod5Config
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force

Powershell DSC 5.0 - 資源的使用

Powershell DSC 5.0 - 資源的使用



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI