Merge pull request #1956 from cchurch/strict_mode_fixes

Update core modules to fix strict mode errors
This commit is contained in:
Brian Coca 2015-09-04 12:48:23 -04:00
commit d55a4d59dc
7 changed files with 65 additions and 125 deletions

View file

@ -60,7 +60,7 @@ Set-Attr $result.ansible_facts "ansible_hostname" $env:COMPUTERNAME;
Set-Attr $result.ansible_facts "ansible_fqdn" "$([System.Net.Dns]::GetHostByName((hostname)).HostName)" Set-Attr $result.ansible_facts "ansible_fqdn" "$([System.Net.Dns]::GetHostByName((hostname)).HostName)"
Set-Attr $result.ansible_facts "ansible_system" $osversion.Platform.ToString() Set-Attr $result.ansible_facts "ansible_system" $osversion.Platform.ToString()
Set-Attr $result.ansible_facts "ansible_os_family" "Windows" Set-Attr $result.ansible_facts "ansible_os_family" "Windows"
Set-Attr $result.ansible_facts "ansible_os_name" $win32_os.Name.Split('|')[0] Set-Attr $result.ansible_facts "ansible_os_name" ($win32_os.Name.Split('|')[0]).Trim()
Set-Attr $result.ansible_facts "ansible_distribution" $osversion.VersionString Set-Attr $result.ansible_facts "ansible_distribution" $osversion.VersionString
Set-Attr $result.ansible_facts "ansible_distribution_version" $osversion.Version.ToString() Set-Attr $result.ansible_facts "ansible_distribution_version" $osversion.Version.ToString()

View file

@ -27,48 +27,18 @@ $result = New-Object PSObject -Property @{
changed = $false changed = $false
} }
If ($params.name) { $name = Get-Attr $params "name" -failifempty $true
$name = $params.name -split ',' | % { $_.Trim() } $name = $name -split ',' | % { $_.Trim() }
}
Else { $state = Get-Attr $params "state" "present"
Fail-Json $result "mising required argument: name" $state = $state.ToString().ToLower()
If (($state -ne 'present') -and ($state -ne 'absent')) {
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
} }
If ($params.state) { $restart = Get-Attr $params "restart" $false | ConvertTo-Bool
$state = $params.state.ToString().ToLower() $includesubfeatures = Get-Attr $params "include_sub_features" $false | ConvertTo-Bool
If (($state -ne 'present') -and ($state -ne 'absent')) { $includemanagementtools = Get-Attr $params "include_management_tools" $false | ConvertTo-Bool
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
}
}
Elseif (!$params.state) {
$state = "present"
}
If ($params.restart) {
$restart = $params.restart | ConvertTo-Bool
}
Else
{
$restart = $false
}
if ($params.include_sub_features)
{
$includesubfeatures = $params.include_sub_features | ConvertTo-Bool
}
Else
{
$includesubfeatures = $false
}
if ($params.include_management_tools)
{
$includemanagementtools = $params.include_management_tools | ConvertTo-Bool
}
Else
{
$includemanagementtools = $false
}
If ($state -eq "present") { If ($state -eq "present") {
try { try {

View file

@ -24,35 +24,31 @@ $params = Parse-Args $args;
$result = New-Object PSObject; $result = New-Object PSObject;
Set-Attr $result "changed" $false; Set-Attr $result "changed" $false;
If (-not $params.name.GetType) { $name = Get-Attr $params "name" -failifempty $true
Fail-Json $result "missing required arguments: name"
$state = Get-Attr $params "state" "present"
$state = $state.ToString().ToLower()
If (($state -ne "present") -and ($state -ne "absent")) {
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
} }
If ($params.state) { $description = Get-Attr $params "description" $null
$state = $params.state.ToString().ToLower()
If (($state -ne "present") -and ($state -ne "absent")) {
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
}
}
Elseif (-not $params.state) {
$state = "present"
}
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" $adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $params.name } $group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $name }
try { try {
If ($state -eq "present") { If ($state -eq "present") {
If (-not $group) { If (-not $group) {
$group = $adsi.Create("Group", $params.name) $group = $adsi.Create("Group", $name)
$group.SetInfo() $group.SetInfo()
Set-Attr $result "changed" $true Set-Attr $result "changed" $true
} }
If ($params.description.GetType) { If ($null -ne $description) {
IF (-not $group.description -or $group.description -ne $params.description) { IF (-not $group.description -or $group.description -ne $description) {
$group.description = $params.description $group.description = $description
$group.SetInfo() $group.SetInfo()
Set-Attr $result "changed" $true Set-Attr $result "changed" $true
} }

View file

@ -21,36 +21,28 @@
$params = Parse-Args $args; $params = Parse-Args $args;
$result = New-Object psobject; $path = Get-Attr $params "path" -failifempty $true
Set-Attr $result "changed" $false; $state = Get-Attr $params "state" "present"
$creates = Get-Attr $params "creates" $false
$extra_args = Get-Attr $params "extra_args" ""
If (-not $params.path.GetType) $result = New-Object psobject @{
{ changed = $false
Fail-Json $result "missing required arguments: path" };
}
$extra_args = "" If (($creates -ne $false) -and ($state -ne "absent") -and (Test-Path $creates))
If ($params.extra_args.GetType)
{ {
$extra_args = $params.extra_args; Exit-Json $result;
}
If ($params.creates.GetType -and $params.state.GetType -and $params.state -ne "absent")
{
If (Test-File $creates)
{
Exit-Json $result;
}
} }
$logfile = [IO.Path]::GetTempFileName(); $logfile = [IO.Path]::GetTempFileName();
if ($params.state.GetType -and $params.state -eq "absent") if ($state -eq "absent")
{ {
msiexec.exe /x $params.path /qb /l $logfile $extra_args; msiexec.exe /x $path /qn /l $logfile $extra_args
} }
Else Else
{ {
msiexec.exe /i $params.path /qb /l $logfile $extra_args; msiexec.exe /i $path /qn /l $logfile $extra_args
} }
Set-Attr $result "changed" $true; Set-Attr $result "changed" $true;

View file

@ -24,26 +24,25 @@ $params = Parse-Args $args;
$result = New-Object PSObject; $result = New-Object PSObject;
Set-Attr $result "changed" $false; Set-Attr $result "changed" $false;
If (-not $params.name.GetType) $name = Get-Attr $params "name" -failifempty $true
{ $state = Get-Attr $params "state" $false
Fail-Json $result "missing required arguments: name" $startMode = Get-Attr $params "start_mode" $false
}
If ($params.state) { If ($state) {
$state = $params.state.ToString().ToLower() $state = $state.ToString().ToLower()
If (($state -ne 'started') -and ($state -ne 'stopped') -and ($state -ne 'restarted')) { If (($state -ne 'started') -and ($state -ne 'stopped') -and ($state -ne 'restarted')) {
Fail-Json $result "state is '$state'; must be 'started', 'stopped', or 'restarted'" Fail-Json $result "state is '$state'; must be 'started', 'stopped', or 'restarted'"
} }
} }
If ($params.start_mode) { If ($startMode) {
$startMode = $params.start_mode.ToString().ToLower() $startMode = $startMode.ToString().ToLower()
If (($startMode -ne 'auto') -and ($startMode -ne 'manual') -and ($startMode -ne 'disabled')) { If (($startMode -ne 'auto') -and ($startMode -ne 'manual') -and ($startMode -ne 'disabled')) {
Fail-Json $result "start mode is '$startMode'; must be 'auto', 'manual', or 'disabled'" Fail-Json $result "start mode is '$startMode'; must be 'auto', 'manual', or 'disabled'"
} }
} }
$svcName = $params.name $svcName = $name
$svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue $svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue
If (-not $svc) { If (-not $svc) {
Fail-Json $result "Service '$svcName' not installed" Fail-Json $result "Service '$svcName' not installed"

View file

@ -42,14 +42,14 @@ If (Test-Path $path)
Set-Attr $result.stat "exists" $TRUE; Set-Attr $result.stat "exists" $TRUE;
$info = Get-Item $path; $info = Get-Item $path;
$epoch_date = Get-Date -Date "01/01/1970" $epoch_date = Get-Date -Date "01/01/1970"
If ($info.Directory) # Only files have the .Directory attribute. If ($info.PSIsContainer)
{ {
Set-Attr $result.stat "isdir" $FALSE; Set-Attr $result.stat "isdir" $TRUE;
Set-Attr $result.stat "size" $info.Length;
} }
Else Else
{ {
Set-Attr $result.stat "isdir" $TRUE; Set-Attr $result.stat "isdir" $FALSE;
Set-Attr $result.stat "size" $info.Length;
} }
Set-Attr $result.stat "extension" $info.Extension; Set-Attr $result.stat "extension" $info.Extension;
Set-Attr $result.stat "attributes" $info.Attributes.ToString(); Set-Attr $result.stat "attributes" $info.Attributes.ToString();

View file

@ -16,7 +16,6 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
######## ########
@ -55,33 +54,21 @@ $result = New-Object psobject @{
changed = $false changed = $false
}; };
If (-not $params.name.GetType) { $username = Get-Attr $params "name" -failifempty $true
Fail-Json $result "missing required arguments: name"
}
$username = Get-Attr $params "name"
$fullname = Get-Attr $params "fullname" $fullname = Get-Attr $params "fullname"
$description = Get-Attr $params "description" $description = Get-Attr $params "description"
$password = Get-Attr $params "password" $password = Get-Attr $params "password"
If ($params.state) { $state = Get-Attr $params "state" "present"
$state = $params.state.ToString().ToLower() $state = $state.ToString().ToLower()
If (($state -ne 'present') -and ($state -ne 'absent') -and ($state -ne 'query')) { If (($state -ne 'present') -and ($state -ne 'absent') -and ($state -ne 'query')) {
Fail-Json $result "state is '$state'; must be 'present', 'absent' or 'query'" Fail-Json $result "state is '$state'; must be 'present', 'absent' or 'query'"
}
}
ElseIf (!$params.state) {
$state = "present"
} }
If ($params.update_password) { $update_password = Get-Attr $params "update_password" "always"
$update_password = $params.update_password.ToString().ToLower() $update_password = $update_password.ToString().ToLower()
If (($update_password -ne 'always') -and ($update_password -ne 'on_create')) { If (($update_password -ne 'always') -and ($update_password -ne 'on_create')) {
Fail-Json $result "update_password is '$update_password'; must be 'always' or 'on_create'" Fail-Json $result "update_password is '$update_password'; must be 'always' or 'on_create'"
}
}
ElseIf (!$params.update_password) {
$update_password = "always"
} }
$password_expired = Get-Attr $params "password_expired" $null $password_expired = Get-Attr $params "password_expired" $null
@ -126,14 +113,10 @@ If ($groups -ne $null) {
} }
} }
If ($params.groups_action) { $groups_action = Get-Attr $params "groups_action" "replace"
$groups_action = $params.groups_action.ToString().ToLower() $groups_action = $groups_action.ToString().ToLower()
If (($groups_action -ne 'replace') -and ($groups_action -ne 'add') -and ($groups_action -ne 'remove')) { If (($groups_action -ne 'replace') -and ($groups_action -ne 'add') -and ($groups_action -ne 'remove')) {
Fail-Json $result "groups_action is '$groups_action'; must be 'replace', 'add' or 'remove'" Fail-Json $result "groups_action is '$groups_action'; must be 'replace', 'add' or 'remove'"
}
}
ElseIf (!$params.groups_action) {
$groups_action = "replace"
} }
$user_obj = Get-User $username $user_obj = Get-User $username
@ -141,7 +124,7 @@ $user_obj = Get-User $username
If ($state -eq 'present') { If ($state -eq 'present') {
# Add or update user # Add or update user
try { try {
If (!$user_obj.GetType) { If (-not $user_obj -or -not $user_obj.GetType) {
$user_obj = $adsi.Create("User", $username) $user_obj = $adsi.Create("User", $username)
If ($password -ne $null) { If ($password -ne $null) {
$user_obj.SetPassword($password) $user_obj.SetPassword($password)
@ -200,13 +183,13 @@ If ($state -eq 'present') {
If ($result.changed) { If ($result.changed) {
$user_obj.SetInfo() $user_obj.SetInfo()
} }
If ($groups.GetType) { If ($null -ne $groups) {
[string[]]$current_groups = $user_obj.Groups() | ForEach { $_.GetType().InvokeMember("Name", "GetProperty", $null, $_, $null) } [string[]]$current_groups = $user_obj.Groups() | ForEach { $_.GetType().InvokeMember("Name", "GetProperty", $null, $_, $null) }
If (($groups_action -eq "remove") -or ($groups_action -eq "replace")) { If (($groups_action -eq "remove") -or ($groups_action -eq "replace")) {
ForEach ($grp in $current_groups) { ForEach ($grp in $current_groups) {
If ((($groups_action -eq "remove") -and ($groups -contains $grp)) -or (($groups_action -eq "replace") -and ($groups -notcontains $grp))) { If ((($groups_action -eq "remove") -and ($groups -contains $grp)) -or (($groups_action -eq "replace") -and ($groups -notcontains $grp))) {
$group_obj = $adsi.Children | where { $_.SchemaClassName -eq 'Group' -and $_.Name -eq $grp } $group_obj = $adsi.Children | where { $_.SchemaClassName -eq 'Group' -and $_.Name -eq $grp }
If ($group_obj.GetType) { If ($group_obj -and $group_obj.GetType) {
$group_obj.Remove($user_obj.Path) $group_obj.Remove($user_obj.Path)
$result.changed = $true $result.changed = $true
} }
@ -239,7 +222,7 @@ If ($state -eq 'present') {
ElseIf ($state -eq 'absent') { ElseIf ($state -eq 'absent') {
# Remove user # Remove user
try { try {
If ($user_obj.GetType) { If ($user_obj -and $user_obj.GetType) {
$username = $user_obj.Name.Value $username = $user_obj.Name.Value
$adsi.delete("User", $user_obj.Name.Value) $adsi.delete("User", $user_obj.Name.Value)
$result.changed = $true $result.changed = $true
@ -252,7 +235,7 @@ ElseIf ($state -eq 'absent') {
} }
try { try {
If ($user_obj.GetType) { If ($user_obj -and $user_obj.GetType) {
$user_obj.RefreshCache() $user_obj.RefreshCache()
Set-Attr $result "name" $user_obj.Name[0] Set-Attr $result "name" $user_obj.Name[0]
Set-Attr $result "fullname" $user_obj.FullName[0] Set-Attr $result "fullname" $user_obj.FullName[0]