PowershellDSC5.0-資源的使用-創(chuàng)新互聯(lián)

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

恩陽網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,恩陽網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為恩陽1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的恩陽做網(wǎ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 - 資源的使用

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

Powershell DSC 5.0 - 資源的使用

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

這個是幾個資源里面相對麻煩的一個,因為默認傳遞的是明文,他不會允許你推送的,我需要明確告訴他允許明文。

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 - 資源的使用

確認成功創(chuàng)建

Powershell DSC 5.0 - 資源的使用

7. 創(chuàng)建進程(運行某個程序) 資源是 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

確認

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. 腳本資源

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

首先通過 test 判斷狀態(tài),返回一個boolean值,如果是true,忽略;如果是false,那么在set里面進行處理;相當于于一個if else的判斷語句。get可以通過getdsc的命令獲取當前狀態(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 - 資源的使用

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章標題:PowershellDSC5.0-資源的使用-創(chuàng)新互聯(lián)
當前路徑:http://www.muchs.cn/article8/dssdop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化企業(yè)網(wǎng)站制作、面包屑導航、App開發(fā)、品牌網(wǎng)站設(shè)計、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)