Skip to content
Permalink
Browse files

Handles:

PSAvoidTrailingWhitespace
PSAvoidGlobalVars
PSAvoidAssignmentToAutomaticVariable
PSAvoidUsingCmdletAliases
PSAvoidUsingWriteHost
PSUseDeclaredVarsMoreThanAssignments
PSUsePSCredentialType
PSAvoidUsingPositionalParameters
PSAvoidUsingEmptyCatchBlock
PSAvoidUsingWMICmdlet

Replaced Write-Host with Write-Output
Added smart reboot check for win_domain feature installation
Modify the Creation of the pagefileto fit to CIM
Changelog fragment addition
Ignore.txt without fixes
  • Loading branch information...
ShachafGoldstein committed Apr 28, 2019
1 parent e105f5b commit 7867de04619fc56ae15bea152bed3c905b3e0f0a
Showing with 216 additions and 286 deletions.
  1. +2 −0 changelogs/fragments/55862-PSLint-errors-fix.yaml
  2. +9 −9 examples/scripts/ConfigureRemotingForAnsible.ps1
  3. +6 −6 examples/scripts/upgrade_to_ps3.ps1
  4. +2 −2 lib/ansible/module_utils/powershell/Ansible.ModuleUtils.CamelConversion.psm1
  5. +1 −2 lib/ansible/module_utils/powershell/Ansible.ModuleUtils.Legacy.psm1
  6. +3 −3 lib/ansible/module_utils/powershell/Ansible.ModuleUtils.LinkUtil.psm1
  7. +2 −2 lib/ansible/module_utils/powershell/Ansible.ModuleUtils.SID.psm1
  8. +17 −15 lib/ansible/modules/windows/setup.ps1
  9. +2 −2 lib/ansible/modules/windows/win_chocolatey_facts.ps1
  10. +1 −1 lib/ansible/modules/windows/win_copy.ps1
  11. +11 −12 lib/ansible/modules/windows/win_credential.ps1
  12. +1 −1 lib/ansible/modules/windows/win_disk_facts.ps1
  13. +1 −1 lib/ansible/modules/windows/win_disk_image.ps1
  14. +10 −9 lib/ansible/modules/windows/win_dns_client.ps1
  15. +6 −1 lib/ansible/modules/windows/win_domain.ps1
  16. +10 −11 lib/ansible/modules/windows/win_domain_controller.ps1
  17. +1 −1 lib/ansible/modules/windows/win_domain_group.ps1
  18. +14 −17 lib/ansible/modules/windows/win_domain_membership.ps1
  19. +1 −1 lib/ansible/modules/windows/win_domain_user.ps1
  20. +3 −2 lib/ansible/modules/windows/win_dsc.ps1
  21. +21 −14 lib/ansible/modules/windows/win_find.ps1
  22. +2 −2 lib/ansible/modules/windows/win_hotfix.ps1
  23. +1 −1 lib/ansible/modules/windows/win_iis_webapppool.ps1
  24. +1 −1 lib/ansible/modules/windows/win_iis_webbinding.ps1
  25. +7 −7 lib/ansible/modules/windows/win_iis_website.ps1
  26. +1 −1 lib/ansible/modules/windows/win_msg.ps1
  27. +3 −3 lib/ansible/modules/windows/win_package.ps1
  28. +14 −17 lib/ansible/modules/windows/win_pagefile.ps1
  29. +2 −2 lib/ansible/modules/windows/win_product_facts.ps1
  30. +4 −4 lib/ansible/modules/windows/win_rabbitmq_plugin.ps1
  31. +1 −1 lib/ansible/modules/windows/win_rds_cap.ps1
  32. +1 −1 lib/ansible/modules/windows/win_rds_rap.ps1
  33. +3 −1 lib/ansible/modules/windows/win_region.ps1
  34. +2 −2 lib/ansible/modules/windows/win_regmerge.ps1
  35. +6 −6 lib/ansible/modules/windows/win_route.ps1
  36. +3 −3 lib/ansible/modules/windows/win_say.ps1
  37. +4 −5 lib/ansible/modules/windows/win_scheduled_task_stat.ps1
  38. +1 −2 lib/ansible/modules/windows/win_security_policy.ps1
  39. +2 −2 lib/ansible/modules/windows/win_shell.ps1
  40. +1 −1 lib/ansible/modules/windows/win_stat.ps1
  41. +3 −2 lib/ansible/modules/windows/win_tempfile.ps1
  42. +7 −7 lib/ansible/modules/windows/win_toast.ps1
  43. +1 −1 lib/ansible/modules/windows/win_unzip.ps1
  44. +1 −0 lib/ansible/modules/windows/win_uri.ps1
  45. +5 −5 lib/ansible/modules/windows/win_user.ps1
  46. +3 −1 lib/ansible/modules/windows/win_wait_for.ps1
  47. +1 −1 test/integration/targets/win_audit_rule/library/test_get_audit_rule.ps1
  48. +1 −1 test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1
  49. +4 −4 test/integration/targets/win_module_utils/library/argv_parser_test.ps1
  50. +1 −1 test/integration/targets/win_module_utils/library/camel_conversion_test.ps1
  51. +1 −1 test/integration/targets/win_ping/library/win_ping_strict_mode_error.ps1
  52. +1 −1 test/integration/targets/win_script/files/test_script.ps1
  53. +1 −1 test/integration/targets/win_script/files/test_script_with_args.ps1
  54. +3 −3 test/integration/targets/win_script/files/test_script_with_splatting.ps1
  55. +0 −83 test/sanity/pslint/ignore.txt
@@ -0,0 +1,2 @@
bugfixes:
- Fixed some PSlint warnings
@@ -93,7 +93,7 @@ Function New-LegacySelfSignedCert
[int]$ValidDays = 1095
)

$hostnonFQDN = $env:computerName
$hostnonFQDN = $env:computerName
$hostFQDN = [System.Net.Dns]::GetHostByName(($env:computerName)).Hostname
$SignatureAlgorithm = "SHA256"

@@ -170,7 +170,7 @@ Function Enable-GlobalHttpFirewallAccess

# try to find/enable the default rule first
$add_rule = $false
$matching_rules = $fw.Rules | ? { $_.Name -eq "Windows Remote Management (HTTP-In)" }
$matching_rules = $fw.Rules | Where-Object { $_.Name -eq "Windows Remote Management (HTTP-In)" }
$rule = $null
If ($matching_rules) {
If ($matching_rules -isnot [Array]) {
@@ -180,7 +180,7 @@ Function Enable-GlobalHttpFirewallAccess
Else {
# try to find one with the All or Public profile first
Write-Verbose "Found multiple existing HTTP firewall rules..."
$rule = $matching_rules | % { $_.Profiles -band 4 }[0]
$rule = $matching_rules | ForEach-Object { $_.Profiles -band 4 }[0]

If (-not $rule -or $rule -is [Array]) {
Write-Verbose "Editing an arbitrary single HTTP firewall rule (multiple existed)"
@@ -310,7 +310,7 @@ if ($token_value -ne 1) {

# Make sure there is a SSL listener.
$listeners = Get-ChildItem WSMan:\localhost\Listener
If (!($listeners | Where {$_.Keys -like "TRANSPORT=HTTPS"}))
If (!($listeners | Where-Object {$_.Keys -like "TRANSPORT=HTTPS"}))
{
# We cannot use New-SelfSignedCertificate on 2012R2 and earlier
$thumbprint = New-LegacySelfSignedCert -SubjectName $SubjectName -ValidDays $CertValidityDays
@@ -363,9 +363,9 @@ Else
# Check for basic authentication.
$basicAuthSetting = Get-ChildItem WSMan:\localhost\Service\Auth | Where-Object {$_.Name -eq "Basic"}

If ($DisableBasicAuth)
If ($DisableBasicAuth)
{
If (($basicAuthSetting.Value) -eq $true)
If (($basicAuthSetting.Value) -eq $true)
{
Write-Verbose "Disabling basic auth support."
Set-Item -Path "WSMan:\localhost\Service\Auth\Basic" -Value $false
@@ -375,8 +375,8 @@ If ($DisableBasicAuth)
{
Write-Verbose "Basic auth is already disabled."
}
}
Else
}
Else
{
If (($basicAuthSetting.Value) -eq $false)
{
@@ -394,7 +394,7 @@ Else
If ($EnableCredSSP)
{
# Check for CredSSP authentication
$credsspAuthSetting = Get-ChildItem WSMan:\localhost\Service\Auth | Where {$_.Name -eq "CredSSP"}
$credsspAuthSetting = Get-ChildItem WSMan:\localhost\Service\Auth | Where-Object {$_.Name -eq "CredSSP"}
If (($credsspAuthSetting.Value) -eq $false)
{
Write-Verbose "Enabling CredSSP auth support."
@@ -5,7 +5,7 @@
# some Ansible modules that may use Powershell 3 features, so systems may need
# to be upgraded. This may be used by a sample playbook. Refer to the windows
# documentation on docs.ansible.com for details.
#
#
# - hosts: windows
# tasks:
# - script: upgrade_to_ps3.ps1
@@ -20,7 +20,7 @@

if ($PSVersionTable.psversion.Major -ge 3)
{
write-host "Powershell 3 Installed already; You don't need this"
Write-Output "Powershell 3 Installed already; You don't need this"
Exit
}

@@ -55,7 +55,7 @@ if (!(test-path $powershellpath))
# If the Operating System is above 6.2, then you already have PowerShell Version > 3
if ([Environment]::OSVersion.Version.Major -gt 6)
{
write-host "OS is new; upgrade not needed."
Write-Output "OS is new; upgrade not needed."
Exit
}

@@ -67,11 +67,11 @@ $architecture = $ENV:PROCESSOR_ARCHITECTURE
if ($architecture -eq "AMD64")
{
$architecture = "x64"
}
}
else
{
$architecture = "x86"
}
$architecture = "x86"
}

if ($osminor -eq 1)
{
@@ -10,7 +10,7 @@ Function Convert-StringToSnakeCase($string) {

# handle when there was nothing before the plural pattern
if ($replacement_string.StartsWith("_") -and -not $string.StartsWith("_")) {
$replacement_string = $replacement_string.Substring(1)
$replacement_string = $replacement_string.Substring(1)
}
$string = $replacement_string
}
@@ -50,7 +50,7 @@ Function Convert-DictToSnakeCase($dict) {

$value = $dict_entry.Value
if ($value -is [Hashtable]) {
$snake_dict.$snake_key = Convert-DictToSnakeCase -dict $value
$snake_dict.$snake_key = Convert-DictToSnakeCase -dict $value
} elseif ($value -is [Array] -or $value -is [System.Collections.ArrayList]) {
$snake_dict.$snake_key = Convert-ListToSnakeCase -list $value
} else {
@@ -360,7 +360,7 @@ Function Get-PendingRebootStatus
Check if reboot is required, if so notify CA.
Function returns true if computer has a pending reboot
#>
$featureData = Invoke-WmiMethod -EA Ignore -Name GetServerFeature -Namespace root\microsoft\windows\servermanager -Class MSFT_ServerManagerTasks
$featureData = Invoke-CimMethod -EA Ignore -Name GetServerFeature -Namespace root\microsoft\windows\servermanager -Class MSFT_ServerManagerTasks
$regData = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" "PendingFileRenameOperations" -EA Ignore
$CBSRebootStatus = Get-ChildItem "HKLM:\\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing" -ErrorAction SilentlyContinue| Where-Object {$_.PSChildName -eq "RebootPending"}
if(($featureData -and $featureData.RequiresReboot) -or $regData -or $CBSRebootStatus)
@@ -375,4 +375,3 @@ Function Get-PendingRebootStatus

# this line must stay at the bottom to ensure all defined module parts are exported
Export-ModuleMember -Alias * -Function * -Cmdlet *

@@ -246,7 +246,7 @@ namespace Ansible
finally
{
FindClose(findHandle);
}
}
}
if (result.Count > 1)
@@ -271,7 +271,7 @@ namespace Ansible
IntPtr.Zero);
if (fileHandle.IsInvalid)
throw new LinkUtilWin32Exception(String.Format("CreateFile({0}) failed", linkPath));
throw new LinkUtilWin32Exception(String.Format("CreateFile({0}) failed", linkPath));
REPARSE_DATA_BUFFER buffer = new REPARSE_DATA_BUFFER();
UInt32 bytesReturned;
@@ -428,7 +428,7 @@ Function New-Link($link_path, $link_target, $link_type) {
if (-not (Test-Path -LiteralPath $link_target)) {
throw "link_target '$link_target' does not exist, cannot create link"
}

switch($link_type) {
"link" {
$type = [Ansible.LinkType]::SymbolicLink
@@ -79,13 +79,13 @@ Function Convert-ToSID {
$account = New-Object System.Security.Principal.NTAccount($username)
}
}

try {
$account_sid = $account.Translate([System.Security.Principal.SecurityIdentifier])
} catch {
Fail-Json @{} "account_name $account_name is not a valid account, cannot get SID: $($_.Exception.Message)"
}

return $account_sid.Value
}

@@ -30,7 +30,7 @@ Function Get-MachineSid {
# admin account (ends with -500) and lops it off to get the machine sid.

$admins_sid = "S-1-5-32-544"
$admin_group = ([Security.Principal.SecurityIdentifier]$admins_sid).Translate([Security.Principal.NTAccount]).Value
$admin_group = ([Security.Principal.SecurityIdentifier]$admins_sid).Translate([Security.Principal.NTAccount]).Value

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$principal_context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
@@ -134,7 +134,7 @@ $osversion = [Environment]::OSVersion

if($gather_subset.Contains('all_ipv4_addresses') -or $gather_subset.Contains('all_ipv6_addresses')) {
$netcfg = Get-LazyCimInstance Win32_NetworkAdapterConfiguration

# TODO: split v4/v6 properly, return in separate keys
$ips = @()
Foreach ($ip in $netcfg.IPAddress) {
@@ -212,9 +212,9 @@ if($gather_subset.Contains('distribution')) {
if($gather_subset.Contains('env')) {
$env_vars = @{ }
foreach ($item in Get-ChildItem Env:) {
$name = $item | select -ExpandProperty Name
$name = $item | Select-Object -ExpandProperty Name
# Powershell ConvertTo-Json fails if string ends with \
$value = ($item | select -ExpandProperty Value).TrimEnd("\")
$value = ($item | Select-Object -ExpandProperty Value).TrimEnd("\")
$env_vars.Add($name, $value)
}

@@ -226,7 +226,7 @@ if($gather_subset.Contains('env')) {
if($gather_subset.Contains('facter')) {
# See if Facter is on the System Path
Try {
$facter_exe = Get-Command facter -ErrorAction Stop
Get-Command facter -ErrorAction Stop
$facter_installed = $true
} Catch {
$facter_installed = $false
@@ -246,15 +246,15 @@ if($gather_subset.Contains('facter')) {
if($gather_subset.Contains('interfaces')) {
$netcfg = Get-LazyCimInstance Win32_NetworkAdapterConfiguration
$ActiveNetcfg = @()
$ActiveNetcfg += $netcfg | where {$_.ipaddress -ne $null}
$ActiveNetcfg += $netcfg | Where-Object {$_.ipaddress -ne $null}

$namespaces = Get-LazyCimInstance __Namespace -namespace root
if ($namespaces | Where-Object { $_.Name -eq "StandardCimv" }) {
$net_adapters = Get-LazyCimInstance MSFT_NetAdapter -namespace Root\StandardCimv2
$guid_key = "InterfaceGUID"
$name_key = "Name"
} else {
$net_adapters = Get-LazyCimInstance Win32_NetworkAdapter
$net_adapters = Get-LazyCimInstance Win32_NetworkAdapter
$guid_key = "GUID"
$name_key = "NetConnectionID"
}
@@ -405,7 +405,7 @@ if($gather_subset.Contains('windows_domain')) {
if($gather_subset.Contains('winrm')) {

$winrm_https_listener_parent_paths = Get-ChildItem -Path WSMan:\localhost\Listener -Recurse -ErrorAction SilentlyContinue | `
Where-Object {$_.PSChildName -eq "Transport" -and $_.Value -eq "HTTPS"} | select PSParentPath
Where-Object {$_.PSChildName -eq "Transport" -and $_.Value -eq "HTTPS"} | Select-Object PSParentPath
if ($winrm_https_listener_parent_paths -isnot [array]) {
$winrm_https_listener_parent_paths = @($winrm_https_listener_parent_paths)
}
@@ -422,14 +422,16 @@ if($gather_subset.Contains('winrm')) {

$winrm_cert_thumbprints = @()
foreach ($https_listener in $https_listeners) {
$winrm_cert_thumbprints += $https_listener | where {$_.Name -EQ "CertificateThumbprint" } | select Value
$winrm_cert_thumbprints += $https_listener | Where-Object {$_.Name -EQ "CertificateThumbprint" } | Select-Object Value
}

$winrm_cert_expiry = @()
foreach ($winrm_cert_thumbprint in $winrm_cert_thumbprints) {
Try {
$winrm_cert_expiry += Get-ChildItem -Path Cert:\LocalMachine\My | where Thumbprint -EQ $winrm_cert_thumbprint.Value.ToString().ToUpper() | select NotAfter
} Catch {}
$winrm_cert_expiry += Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object Thumbprint -EQ $winrm_cert_thumbprint.Value.ToString().ToUpper() | Select-Object NotAfter
} Catch {
Add-Warning -obj $result -message "Error during certificate expiration retrieval"
}
}

$winrm_cert_expirations = $winrm_cert_expiry | Sort-Object NotAfter
@@ -453,22 +455,22 @@ if($gather_subset.Contains('virtual')) {
$machine_role="guest"
}

"VirtualBox" {
$machine_type="VirtualBox"
"VirtualBox" {
$machine_type="VirtualBox"
$machine_role="guest"
}

"HVM domU" {
$machine_type="Xen"
$machine_role="guest"
$machine_role="guest"
}

default {
$machine_type="NA"
$machine_role="NA"
}
}

$ansible_facts += @{
ansible_virtualization_role = $machine_role
ansible_virtualization_type = $machine_type
@@ -1,8 +1,8 @@
#!powershell

# Copyright: (c) 2018, Ansible Project
# Copyright: (c) 2018, Simon Baerlocher <s.baerlocher@sbaerlocher.ch>
# Copyright: (c) 2018, ITIGO AG <opensource@itigo.ch>
# Copyright: (c) 2018, Simon Baerlocher <s.baerlocher@sbaerlocher.ch>
# Copyright: (c) 2018, ITIGO AG <opensource@itigo.ch>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

#Requires -Module Ansible.ModuleUtils.ArgvParser
@@ -34,7 +34,7 @@ $force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $true
# used in query mode, contains the local files/directories/symlinks that are to be copied
$files = Get-AnsibleParam -obj $params -name "files" -type "list"
$directories = Get-AnsibleParam -obj $params -name "directories" -type "list"
$symlinks = Get-AnsibleParam -obj $params -name "symlinks" -type "list"
#$symlinks = Get-AnsibleParam -obj $params -name "symlinks" -type "list"

$result = @{
changed = $false
@@ -513,19 +513,19 @@ Function ConvertTo-CredentialAttribute {
}

Function Get-DiffInfo {
param($Credential)
param($Ansible_Credential)

$diff = @{
alias = $Credential.TargetAlias
alias = $Ansible_Credential.TargetAlias
attributes = [System.Collections.ArrayList]@()
comment = $Credential.Comment
name = $Credential.TargetName
persistence = $Credential.Persist.ToString()
type = $Credential.Type.ToString()
username = $Credential.UserName
comment = $Ansible_Credential.Comment
name = $Ansible_Credential.TargetName
persistence = $Ansible_Credential.Persist.ToString()
type = $Ansible_Credential.Type.ToString()
username = $Ansible_Credential.UserName
}

foreach ($attribute in $Credential.Attributes) {
foreach ($attribute in $Ansible_Credential.Attributes) {
$attribute_info = @{
name = $attribute.Keyword
data = $null
@@ -573,7 +573,7 @@ $type = switch ($type) {

$existing_credential = [Ansible.CredentialManager.Credential]::GetCredential($name, $type)
if ($null -ne $existing_credential) {
$module.Diff.before = Get-DiffInfo -Credential $existing_credential
$module.Diff.before = Get-DiffInfo -Ansible_Credential $existing_credential
}

if ($state -eq "absent") {
@@ -654,7 +654,7 @@ if ($state -eq "absent") {
if (($new_keyword -cne $existing_keyword) -or ($new_value -ne $existing_value)) {
$attribute_changed = $true
break
}
}
}
}

@@ -705,9 +705,8 @@ if ($state -eq "absent") {
} else {
# Get a new copy of the credential and use that to set the after diff
$new_credential = [Ansible.CredentialManager.Credential]::GetCredential($name, $type)
$module.Diff.after = Get-DiffInfo -Credential $new_credential
$module.Diff.after = Get-DiffInfo -Ansible_Credential $new_credential
}
}

$module.ExitJson()

@@ -36,7 +36,7 @@ try {
} catch {
Fail-Json -obj $result -message "Failed to search the disks on the target: $($_.Exception.Message)"
}
[int32]$diskcount = $disks | Measure-Object | Select-Object -ExpandProperty Count
#[int32]$diskcount = $disks | Measure-Object | Select-Object -ExpandProperty Count
foreach ($disk in $disks) {
$disk_info = @{}
$pdisk = Get-PhysicalDisk -ErrorAction SilentlyContinue | Where-Object {
@@ -32,7 +32,7 @@ If($state -eq "present") {
# the actual mount is async, so the CIMInstance result may not immediately contain the data we need
$retry_count = 0
While(-not $di.Attached -and $retry_count -lt 5) {
Sleep -Seconds 1 | Out-Null
Start-Sleep -Seconds 1 | Out-Null
$di = $di | Get-DiskImage
$retry_count++
}

0 comments on commit 7867de0

Please sign in to comment.
You can’t perform that action at this time.