Le script ci-dessous est un exemple simple de l'utilisation de l'API Rest de Vcenter pour recuperer toute sorte d'information, nottament sur les machines virtuelles.
A noter que la limitation du nombre d'objet retournés oblige simplement a recuperer dans un premier temps la liste des hosts, puis par host la liste des VMs.
### QUERY VCENTER REST API TO GET VM LIST ###
$user = ‘myaccount’
$pswd = Read-Host -Prompt "Enter Password"
$vCenterName = ‘MyVcenter’
$encoded = [System.Text.Encoding]::UTF8.GetBytes(($user, $pswd -Join ‘:’))
$encodedPassword = [System.Convert]::ToBase64String($Encoded)
$authHeader = @{
Authorization = "Basic $($EncodedPassword)"
}
$sRest = @{
Method = ‘Post’
Uri = "https://$($vCenterName)/rest/com/vmware/cis/session"
Headers = $authHeader
}
$result = Invoke-RestMethod @sRest
# Get TokenID
$authHeader = @{
‘vmware-api-session-id’ = $result.value
}
# Get All Hosts
$gethosts = "https://$($vCenterName)/rest/vcenter/host"
$resultgethost = Invoke-RestMethod -Uri $gethosts -Headers $authHeader
$hostidlist = $resultgethost.value.host
# For each host, get all VMs
foreach ($hostid in $hostidlist)
{
$get_vm = "https://$($vCenterName)/rest/vcenter/vm?filter.hosts=$hostid"
$resultvm = Invoke-RestMethod -Uri $get_vm -Headers $authHeader
[array]$FinalTableau += $resultvm
}
# Display Result
$FinalTableau.value | select name,power_state
# Output to CSV
$CsvTab = $FinalTableau.value | select name,power_state | ConvertTo-Csv -Delimiter ';' -NoTypeInformation
$CsvTab | Out-File .\VMList.csv
Dans le cadre de l'automatisation, il arrive de recevoir une demande pour développer un script PowerShell pour le nettoyage ou la gestion de l'obsolescence des utilisateurs dans l'Active Directory.
Dans le script, vous aurez peut être besoin de comparer la date du last logon utilisateur dans l'Active Directory avec une date manuellement saisie (variable du script) avec un format spécifique.
Ci-dessous un exemple de PowerShell pour comparer une date donnée avec une date de last logon utilisateur dans Active Directory :
# Import Active Directory Module
Import-Module ActiveDirectory
#Set a fixed date for comparison
$Fixed_Date = "13/11/2021"
#Convert the fixed date from a string to a compatible specific format
$Fixed_Date_Convert = [datetime]::parseexact($Fixed_Date, 'dd/MM/yyyy', $null)
#Get AD user last logon date in dd/MM/yyyy format
$ADUser = get-aduser "jdoe" -Properties lastlogondate | select @{Name=”ModifiedLastLogonDate”;Expression={$_.LastLogonDate.ToString(“dd/MM/yyyy”)}}
#Convert AD last logon date to a specific format to be compared with the chosen date above
$ADdate = [datetime]::parseexact($ADUser.ModifiedLastLogonDate, 'dd/MM/yyyy', $null)
if($ADdate -le $Fixed_Date_Convert)
{
echo "Active directory last logon date is inferior to the fixed date"
}
else
{
echo "Active directory last logon date is superior to the fixed date"
}
Conclusion:
La méthode [datetime]::parseexact(dateString, format, provider) Convertit la représentation sous forme de chaîne spécifiée d'une date et d'une heure en son équivalent DateTime.
Ci-dessous, une série de requêtes SQL sur les déploiements, les assignations et les mise a jours.
/**** QUERIES: DEPLOYMENTS,ASSIGNMENTS,UPDATES ****/
--- LIST OF DEPLOYMENTS NAME
SELECT
--DeploymentID = cia.Assignment_UniqueID,
DeploymentName = isnull(grp.Title+' - ', '') + cia.AssignmentName
FROM [dbo].[vCI_CIAssignments] CIA
LEFT JOIN [dbo].[vCI_AssignmentTargetedGroups] ATG on CIA.AssignmentType=5 and ATG.AssignmentID=CIA.AssignmentID
LEFT JOIN [dbo].[v_AuthListInfo] GRP on CIA.AssignmentType=5 and GRP.CI_ID=ATG.AssignedUpdateGroup
WHERE CIA.AssignmentType in (1,5)
--- LIST OF ASSIGNMENTS NAME WITH RELATED DEPLOYMENTS NAME
SELECT
--AssignmentID,
CIA.AssignmentName,
--,DEPLOY.DeploymentID,
DEPLOY.DeploymentName
FROM v_CIAssignment CIA
INNER JOIN (
SELECT DeploymentID = cia.Assignment_UniqueID
, DeploymentName = isnull(grp.Title+' - ', '') + cia.AssignmentName
FROM [dbo].[vCI_CIAssignments] CIA
LEFT JOIN[dbo].[vCI_AssignmentTargetedGroups] ATG on CIA.AssignmentType=5 and ATG.AssignmentID=CIA.AssignmentID
LEFT JOIN [dbo].[v_AuthListInfo] GRP on CIA.AssignmentType=5 and GRP.CI_ID=ATG.AssignedUpdateGroup
WHERE CIA.AssignmentType in (1,5)
) DEPLOY on DEPLOY.DeploymentID = CIA.Assignment_UniqueID
---- LIST OF UPDATES
SELECT DISTINCT
--CI_UniqueID,
DisplayTitle = ui.Title+isnull(' ('+nullif(ui.ArticleID, '')+')', '') + isnull(' ('+nullif(ui.BulletinID, '') +')', '')
,DateCreated
, (CASE ui.Severity
WHEN 0 THEN 'NONE'
WHEN 2 THEN 'LOW'
WHEN 6 THEN 'MODERATE'
WHEN 8 THEN 'IMPORTANT'
WHEN 10 THEN 'CRITICAL' END) AS SEVERITY
FROM v_UpdateInfo UI
ORDER BY DateCreated DESC
---- LIST OF UPDATES AND RELATED ASSIGNMENTS NAME
SELECT DISTINCT
--CI_UniqueID,
DisplayTitle = ui.Title+isnull(' ('+nullif(ui.ArticleID, '')+')', '') + isnull(' ('+nullif(ui.BulletinID, '') +')', '')
,DateCreated
,(CASE ui.Severity
WHEN 0 THEN 'NONE'
WHEN 2 THEN 'LOW'
WHEN 6 THEN 'MODERATE'
WHEN 8 THEN 'IMPORTANT'
WHEN 10 THEN 'CRITICAL' END) AS SEVERITY
,CIA.AssignmentName
FROM v_CIAssignment CIA
INNER JOIN v_CIAssignmentToCI ATC on ATC.AssignmentID=CIA.AssignmentID
INNER JOIN v_UpdateInfo UI on UI.CI_ID=ATC.CI_ID
ORDER BY DateCreated DESC
---- LIST OF UPDATES AND RELATED DEPLOYMENTS NAME
SELECT
--CI_UniqueID,
DisplayTitle = ui.Title+isnull(' ('+nullif(ui.ArticleID, '')+')', '') + isnull(' ('+nullif(ui.BulletinID, '') +')', '')
,UI.DateCreated
,(CASE ui.Severity
WHEN 0 THEN 'NONE'
WHEN 2 THEN 'LOW'
WHEN 6 THEN 'MODERATE'
WHEN 8 THEN 'IMPORTANT'
WHEN 10 THEN 'CRITICAL' END) AS SEVERITY
,DEPLOY.DeploymentName
FROM v_CIAssignment CIA
INNER JOIN v_CIAssignmentToCI ATC on ATC.AssignmentID=CIA.AssignmentID
INNER JOIN v_UpdateInfo UI on UI.CI_ID=ATC.CI_ID
INNER JOIN (
SELECT DeploymentID = cia.Assignment_UniqueID
, DeploymentName = isnull(grp.Title+' - ', '') + cia.AssignmentName
FROM [dbo].[vCI_CIAssignments] CIA
LEFT JOIN [dbo].[vCI_AssignmentTargetedGroups] ATG on CIA.AssignmentType=5 and ATG.AssignmentID=CIA.AssignmentID
LEFT JOIN [dbo].[v_AuthListInfo] GRP on CIA.AssignmentType=5 and GRP.CI_ID=ATG.AssignedUpdateGroup
WHERE CIA.AssignmentType in (1,5)
) DEPLOY on DEPLOY.DeploymentID = CIA.Assignment_UniqueID
ORDER BY DateCreated DESC