a ì)gp,ã@s ddlmZmZmZeZdZddlZddlZddl Z ddl Z ddl Z ddl m mZddlZddlmZmZddlmZgd¢Zd dd „ZGd d „d eƒZdS) é)Úabsolute_importÚdivisionÚprint_functionaL name: powershell version_added: historical short_description: Windows PowerShell description: - The only option when using 'winrm' or 'psrp' as a connection plugin. - Can also be used when using 'ssh' as a connection plugin and the C(DefaultShell) has been configured to PowerShell. extends_documentation_fragment: - shell_windows N)Úto_bytesÚto_text)Ú ShellBase)Z PowerShellz -NoProfilez-NonInteractivez-ExecutionPolicyZ UnrestrictedÚErrorc s˜g}|rŠ| d¡d}|| d¡|…}||d…}t |¡}t d|j¡}|r^d| d¡nd}| d |¡}| ‡fd d „|Dƒ¡qt d   |¡ƒS) z¼ Takes a byte string like '#< CLIXML és7óz!_parse_clixml..z ) ÚfindÚETZ fromstringÚreÚmatchÚtagÚgroupÚfindallÚextendrÚjoin) ÚdatarÚlinesZend_idxZcurrent_elementZclixmlZnamespace_matchÚ namespaceÚstringsrrrÚ _parse_clixml!s  r%c@s´eZdZeƒZdZdZdZdZdd„Z dd„Z d d „Z d d „Z d d„Z dd„Zdd„Zd*dd„Zd+dd„Zd,dd„Zdd„Zdd„Zd-d d!„Zd"d#„Zd$d%„Zd&d'„Zd.d(d)„ZdS)/Ú ShellModuleZ powershellz> $nullú;TcKsdS)Nr r)ÚselfÚkwargsrrrÚ env_prefixMszShellModule.env_prefixcs:‡fdd„|Dƒ}tj|dgdd„|dd…Dƒ¢RŽS)Ncsg|]}t ˆ |¡¡‘qSr)ÚntpathÚnormpathÚ_unquote)rÚarg©r(rrrSrz)ShellModule.join_path..rcSsg|]}| d¡‘qS)ú\©Ústrip)rÚpartrrrrXrr )r+r )r(ÚargsÚpartsrr/rÚ join_pathQszShellModule.join_pathcCs@tj | ¡¡}tj | ¡¡\}}| ¡dvr8|dS| ¡S)N)ú.ps1z.exer7)ÚosÚpathÚbasenamer2ÚsplitextÚlower)r(ÚpathnameÚ base_nameÚnameÚextrrrÚget_remote_filenameZs  zShellModule.get_remote_filenamecCs| |¡}| d¡p| d¡S)Nú/r0)r-Úendswith)r(r9rrrÚpath_has_trailing_slashcs z#ShellModule.path_has_trailing_slashcCs tdƒ‚dS)Nz'chmod is not implemented for Powershell©ÚNotImplementedError)r(ÚpathsÚmoderrrÚchmodhszShellModule.chmodcCs tdƒ‚dS)Nz'chown is not implemented for PowershellrE)r(rGÚuserrrrÚchownkszShellModule.chowncCs tdƒ‚dS)Nz/set_user_facl is not implemented for PowershellrE)r(rGrJrHrrrÚ set_user_faclnszShellModule.set_user_faclFcCs4| | |¡¡}|r"| d|¡S| d|¡SdS)Nz!Remove-Item '%s' -Force -Recurse;zRemove-Item '%s' -Force;©Ú_escaper-Ú_encode_script)r(r9ZrecurserrrÚremoveqszShellModule.removeNcCsJ|s|j ¡}| | |¡¡}|r&|n| d¡}d||f}| | ¡¡S)NZ remote_tmpzÇ $tmp_path = [System.Environment]::ExpandEnvironmentVariables('%s') $tmp = New-Item -Type Directory -Path $tmp_path -Name '%s' Write-Output -InputObject $tmp.FullName )Ú __class__Z_generate_temp_dir_namerNr-Z get_optionrOr2)r(ZbasefileÚsystemrHZtmpdirZ basetmpdirÚscriptrrrÚmkdtempxs üzShellModule.mkdtempr cCsR| |¡}|dkrd}n0| d¡r:d| |dd…¡}nd| |¡}| |¡S)Nú~z Write-Output (Get-Location).Pathz~\z)Write-Output ((Get-Location).Path + '%s')r zWrite-Output '%s')r-Ú startswithrNrO)r(Zuser_home_pathÚusernamerSrrrÚ expand_user‡s  zShellModule.expand_usercCs"| | |¡¡}d|}| |¡S)Nzß If (Test-Path '%s') { $res = 0; } Else { $res = 1; } Write-Output '$res'; Exit $res; rM)r(r9rSrrrÚexists”s  õ zShellModule.existscOs(| | |¡¡}dt|d}| |¡S)Na} If (Test-Path -PathType Leaf '%(path)s') { $sp = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider; $fp = [System.IO.File]::Open('%(path)s', [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read); [System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower(); $fp.Dispose(); } ElseIf (Test-Path -PathType Container '%(path)s') { Write-Output "3"; } Else { Write-Output "1"; } )r9)rNr-ÚdictrO)r(r9r4r)rSrrrÚchecksum¤s ðzShellModule.checksumc Cst dd¡}|dkr$|j|dddStj|dd}ttt|ƒƒ}|r¢| ¡dkr¢|  |d¡ ¡  d ¡s~d |  |d¡|d<d |dd |j|ddd}|S|rÆ|  d ¡rÆ|  d|dd…¡n |sæ|  |d¡|d<|  |¡d|d |¡f}|j|ddS)Nzansible.executor.powershellzbootstrap_wrapper.ps1r F)rSÚ strict_modeÚ preserve_rc)Úposixz #!powershellrr7z"%s.ps1"ztype z | z#!éa Try { %s %s } Catch { $_obj = @{ failed = $true } If ($_.Exception.GetType) { $_obj.Add('msg', $_.Exception.Message) } Else { $_obj.Add('msg', $_.ToString()) } If ($_.InvocationInfo.PositionMessage) { $_obj.Add('exception', $_.InvocationInfo.PositionMessage) } ElseIf ($_.ScriptStackTrace) { $_obj.Add('exception', $_.ScriptStackTrace) } Try { $_obj.Add('error_record', ($_ | ConvertTo-Json | ConvertFrom-Json)) } Catch { } Echo $_obj | ConvertTo-Json -Compress -Depth 99 Exit 1 } ú )r])ÚpkgutilÚget_datarOÚshlexÚsplitÚlistÚmaprr<r-rCrVÚinsertÚappendr ) r(Z env_stringZshebangÚcmdZarg_pathZbootstrap_wrapperÚ cmd_partsZ wrapper_cmdrSrrrÚbuild_module_command¹s&   # Ý$z ShellModule.build_module_commandcCsd|S)Nz& %s; exit $LASTEXITCODEr)r(rirrrÚ wrap_for_execöszShellModule.wrap_for_execcCsDt|pdƒ}t d|¡}|r&| d¡St d|¡}|r@| d¡S|S)z5Remove any matching quotes that wrap the given value.r z^\s*?\'(.*?)\'\s*?$r z^\s*?"(.*?)"\s*?$)rrrr)r(ÚvalueÚmrrrr-ùs     zShellModule._unquotecCst d¡ d|¡S)z9Return value escaped for use in PowerShell single quotes.u(['‘’‚‛])z\1\1)rÚcompileÚsub)r(rmrrrrNszShellModule._escapecCs‚t|ƒ}|dkrtddg}nR|r*d|}|r6d|}d dd„| ¡Dƒ¡}tt | d¡¡d ƒ}td |g}|rx|Sd  |¡S) z?Convert a PowerShell script to a single base64-encoded command.ú-z-Commandz"Set-StrictMode -Version Latest %szz%s If (-not $?) { If (Get-Variable LASTEXITCODE -ErrorAction SilentlyContinue) { exit $LASTEXITCODE } Else { exit 1 } } Ú cSsg|]}| ¡r| ¡‘qSrr1)rÚxrrrrrz.ShellModule._encode_script..z utf-16-lezutf-8z-EncodedCommandr`)rÚ _common_argsr Ú splitlinesÚbase64Z b64encodeÚencode)r(rSZas_listr\r]rjZencoded_scriptrrrrO sÿ zShellModule._encode_script)F)NFNN)r )N)FTT)Ú__name__Ú __module__Ú __qualname__Ú frozensetZCOMPATIBLE_SHELLSZ SHELL_FAMILYZ_SHELL_REDIRECT_ALLNULLZ _SHELL_ANDZ _IS_WINDOWSr*r6rArDrIrKrLrPrTrXrYr[rkrlr-rNrOrrrrr&<s,      = r&)r)Z __future__rrrÚtypeZ __metaclass__Z DOCUMENTATIONrvr8rrcraZxml.etree.ElementTreeZetreeZ ElementTreerr+Zansible.module_utils._textrrZansible.plugins.shellrrtr%r&rrrrÚs