Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Powershell DSC 5. 0-use of resources

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Powershell DSC comes with some common resources. If you need something new, you can download and install DSC Resource Kit 10 or download it from Powershell Gallery.

For example, check the currently installed resources

PS C:\ Windows\ system32 > Get-DscResourceImplementedAs 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...}

Here are some common examples:

Copy file resources are called 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\ Mod5ConfigStart-DscConfiguration-computername sydittest-Path c:\ dsc\ Mod5Config-Wait-Verbose-force

Check that the zip file has been copied

two。 Extract the zip file, and the resource is called Archive

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

The file has been extracted to the specified directory

3. Create an environment variable resource called Environment

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

Confirm that the environment variable has been created

4. Creating a local user resource is User

This is a relatively troublesome one of several resources, because the default transmission is plaintext, he will not allow you to push, I need to explicitly tell him to allow plaintext.

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

It can be seen that it is sent in clear text. If it is a production environment, it needs to be encrypted with a certificate.

Push out

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

You can see that the user has created

5. Create a local group resource Group

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

6. Create registry key resource Registry

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

Confirm successful creation

7. Create a process (run a program) the resource is Windowsprocess

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

confirm

8. Change the status of the service

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

Before Chan

Change

After Chan

9. Script resources

This is when the existing resources can not meet the needs, you can customize the resources. There are three sub-modules in it, test,set,get

First of all, the state is judged by test and a Boolean value is returned, which is ignored if it is true; if it is false, it is processed in set; it is equivalent to a judgment statement of if else. Get can get the current status through the getdsc command

The following example is to determine if the bits service is turned on, then shut down

Configuration TestScript {Node sydittest {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\ Mod5ConfigStart-DscConfiguration-computername sydittest-Path c:\ dsc\ Mod5Config-Wait-Verbose-force

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report