Question Méthode pour explorer un partage avec credentiels

Plus d'informations
il y a 15 ans 9 mois #7067 par SAINTHERAND
Bonjour, je suis nouveau sur ce forum et j'aimerais poursuivre ma formation POWERSHELL avec votre aide..

J'essai de scripter le fait de pouvoir explorer un partage DFS avec un compte de domaine ayant les droits.

+######################################################################+

param($CodeSalarie = $(Read-Host -prompt \"Saisir le Code Salarié\"))
$InfoDFS = dfscmd /view \"\\server\share\" /full | Select-String -pattern $CodeSalarie
[STRING]$RedirDFS = $InfoDFS[1]
$path = $RedirDFS.TrimStart()

Write-Host `n+

+
Write-Host \"+ Le lien DFS \" -nonewline ; Write-Host -foreground red Documents -nonewline ; Write-Host \" pour \" -nonewline ; Write-Host -foreground red $codesalarie -nonewline ; Write-Host \" est : \" -nonewline ; Write-Host -foreground red $InfoDFS[0]
Write-Host +
+
Write-Host \"+ La cible DFS pour \" -nonewline ; Write-Host -foreground red $codesalarie -nonewline ; Write-Host \" est : \" -nonewline ; Write-Host -foreground red $path
Write-Host +
+

###PREMIERE METHODE ESSAYE#################################################
# Mais cette méthode impose de monter un lecteur redirigé vers le partage #
# Mais c'est la seule méthode que je trouve pour préciser mes crédentiels #
###########################################################################

$cred = get-credential \"domain\user\"
$drive = \"X:\"
$user = $cred.UserName
$net = New-Object -com WScript.Network
$pwd = $cred.Password
$net.RemoveNetworkDrive($drive)
$net.MapNetworkDrive($drive, $path , $true , $user, $cred.GetNetworkCredential().Password)

## Donc là je peux naviguer sur mon poste de travail pour bosser sur le lecteur X:\ avec les bons droits.
## Je souhaitais gérer le fait de démonter le lecteur après avoir bosser sur la ressource mais je peux pas le prévoir dans le script....
## Donc je pensais utiliser une autre méthode que le montage de lecteur pour pouvoir explorer le dossier partagé sans monter de lecteur...

## PowerShell Shell.Application - Appel OBJET COM
$ShellExplore = New-Object -comObject Shell.Application
$ShellExplore.Open($path)

## La Méthode .Open de l'objet Shell.Application ne permet pas de préciser des crédentiels.... ##

+######################################################################+

J'espère que vous aurez une piste de réflexion à m'apporter pour avancer dans ce premier script...

Merci d'avance...




[/quote]

Message édité par: TSAINTH, à: 4/06/10 13:57

Message édité par: TSAINTH, à: 4/06/10 15:01

Message édité par: TSAINTH, à: 4/06/10 15:29<br><br>Message édité par: TSAINTH, à: 8/06/10 08:59

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #7111 par SAINTHERAND
Bonjour à tous, bon je vois que ma problématique n'évoque pas forcément de solution à vos yeux...

Je vais tenter de faire différemment....tant pis...

Merci

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #7117 par Laurent Dardenne
TSAINTH écrit:

Bonjour à tous, bon je vois que ma problématique n'évoque pas forcément de solution à vos yeux...

Je ne connais pas DFS, mais plutot MSDN, et je n'y ai pas trouvé d'API qui pourraient t'aider à réaliser ce que tu veux faire. En même temps dans DFS, il y a File System ( j'ai bon ?:lol: ) ce qui à mon avis sous entend que l'on est obligé d'utiliser un lecteur ou un point de jonction (DFS est en gros un redirecteur).
Je n'ai pas trouvé de provider PowerShell pour DFS.
As-tu essayé avec des accès UNC ?

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #7123 par Jireck
[code:1]## Check-DFSR.ps1 script
## Written by: Mark A. Weaver
## Site: www.vmweaver.com
## Version: 2.0
## Date: 5/7/2009
## Purpose: This script will query the local WMI root for DFS replication groups and folders.
## It will then run DFS utilities to determine the number of files in the backlog on the
## destination partners in the replication group.
##
## This script was written for the spefic use of being run on a centralized DFSR server
## which acts as the HUB for remote office backups.
##
##
## Monitoring Rules can be setup to collect and report on the events being generated.
##
## Event information is written to the Application log using the EventIDs at the bottom.
## Input: None
#############################
## Updates:
## 20090408 Weaver: Fixed issue where multiple events are generated throughout the execution
## 20090408 Weaver: Added BacklogFileCount to event message
## 20090409 Weaver: Fixed list of replication connections issue due to change in replication topology
## 20090507 Weaver: Added functionality to return results from all partners in the replication
##
##
######################################################################
######################################################################
# Write-Event powershell function
# Written by Mike Hays
# blog.mike-hays.net
#
#

function Write-Event(
[string]$Source = $(throw \&quot;An event Source must be specified.\&quot;«»),
[int]$EventId = $(throw \&quot;An Event ID must be specified.\&quot;«»),
[System.Diagnostics.EventLogEntryType] $EventType = $(throw \&quot;Event EventType must be specified. (Error, Warning, Information, SuccessAudit, FailureAudit)\&quot;«»),
[string]$Message = $(throw \&quot;An event Message must be specified.\&quot;«»),
$EventLog
)
{
#Uncommon event logs can be specified (even custom ones), but since that isn't generally
#the desired result, I prevent that here
$acceptedEventLogs = \&quot;Application\&quot;, \&quot;System\&quot;
if ($eventEventLog -eq $null)
{
$eventEventLog = \&quot;Application\&quot;
}
elseif (!($acceptedEventLogs -icontains $eventEventLog))
{
Write-Host \&quot;This function supports writing to the following event logs:\&quot; $acceptedEventLogs
Write-Host \&quot;Defaulting to Application Eventlog\&quot;
$eventEventLog = \&quot;Application\&quot;
}

#Create a .NET object that is connected to the Eventlog
$event = New-Object -type System.Diagnostics.Eventlog -argumentlist $EventLog
#Define the Source property
$event.Source = $Source
#Write the event to the log
$event.WriteEntry($Message, $EventType, $EventId)
}

######################################################################
######################################################################
## Main
## Errors written:
## Log File: Application
## Source: Check-DFSR Script
## ID: 9500 - Lists fully replicated replication folders
## ID: 9501 - Lists replication folders with less than the $BacklogErrorLevel files waiting
## ID: 9502 - Lists replication folders with more than the $BacklogErrorLevel files waiting
## ID: 9503 - If a connection is not pingable, this event is written.

$BacklogErrorLevel = 10

$ComputerName = $env:ComputerName
## Query DFSR groups from the local MicrosftDFS WMI namespace.
$DFSRGroupWMIQuery = \&quot;SELECT * FROM DfsrReplicationGroupConfig\&quot;
$RGroups = Get-WmiObject -Namespace \&quot;root\MicrosoftDFS\&quot; -Query $DFSRGroupWMIQuery


## Setup my variables
$ping = New-Object System.Net.NetworkInformation.Ping
$SuccessAudit = $Null
$WarningAudit = $Null
$ErrorAudit = $Null
$EventSource = \&quot;Check-DFSR Script\&quot;
$SuccessEventID = 9500
$WarningEventID = 9501
$ErrorEventID = 9502
$NoPingEventID = 9503

foreach ($Group in $RGroups)
{
## Cycle through all Replication groups found
$DFSRGFoldersWMIQuery = \&quot;SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='\&quot; + $Group.ReplicationGroupGUID + \&quot;'\&quot;
$RGFolders = Get-WmiObject -Namespace \&quot;root\MicrosoftDFS\&quot; -Query $DFSRGFoldersWMIQuery

## Grab all connections associated with a Replication Group
$DFSRConnectionWMIQuery = \&quot;SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='\&quot; + $Group.ReplicationGroupGUID + \&quot;'\&quot;
$RGConnections = Get-WmiObject -Namespace \&quot;root\MicrosoftDFS\&quot; -Query $DFSRConnectionWMIQuery
foreach ($Connection in $RGConnections)
{

$ConnectionName = $Connection.PartnerName.Trim()
$IsInBound = $Connection.Inbound
$IsEnabled = $Connection.Enabled

## Do not attempt to look at connections that are Disabled
if ($IsEnabled -eq $True)
{
## If the connection is not ping-able, do not attempt to query it for Backlog info
$Reply = $ping.send(\&quot;$ConnectionName\&quot;«»)
if ($reply.Status -eq \&quot;Success\&quot;«»)
{


## Cycle through the Replication Folders that are part of the replication group and run DFSRDIAG tool to determine the backlog on the connection partners.
foreach ($Folder in $RGFolders)
{
$RGName = $Group.ReplicationGroupName
$RFName = $Folder.ReplicatedFolderName

## Determine if current connect is an inbound connection or not, set send/receive members accordingly
if ($IsInBound -eq $True)
{
$SendingMember = $ConnectionName
$ReceivingMember = $ComputerName
}
else
{
$SendingMember = $ComputerName
$ReceivingMember = $ConnectionName
}
$Out = $RGName + \&quot;:\&quot; + $RFName + \&quot; - S:\&quot;+$SendingMember + \&quot; R:\&quot; + $ReceivingMember
Write-Host $Out
## Execute the dfsrdiag command and get results back in the $Backlog variable
$BLCommand = \&quot;dfsrdiag Backlog /RGName:'\&quot; + $RGName + \&quot;' /RFName:'\&quot; + $RFName + \&quot;' /SendingMember:\&quot; + $SendingMember + \&quot; /ReceivingMember:\&quot; + $ReceivingMember
$Backlog = Invoke-Expression -Command $BLCommand

$BackLogFilecount = 0
foreach ($item in $Backlog)
{
if ($item -ilike \&quot;*Backlog File count*\&quot;«»)
{
$BacklogFileCount = [int]$Item.Split(\&quot;:\&quot;«»)[1].Trim()
}

}


if ($BacklogFileCount -eq 0)
{
#Update Success Audit
$SuccessAudit += $RGName + \&quot;:\&quot; + $RFName + \&quot; is in sync with 0 files in the backlog from \&quot;+ $SendingMember + \&quot; to \&quot; + $ReceivingMember +\&quot;.`n\&quot;

}
elseif ($BacklogFilecount -lt $BacklogErrorLevel)
{
#Update Warning Audit
$WarningAudit += $RGName + \&quot;:\&quot; + $RFName + \&quot; has \&quot; + $BacklogFileCount + \&quot; files in the backlog from \&quot; + $SendingMember + \&quot; to \&quot; + $ReceivingMember + \&quot;.`n\&quot;
}
else
{
#Update Error Audit
$ErrorAudit += $RGName + \&quot;:\&quot; + $RFName + \&quot; has \&quot; + $BacklogFilecount + \&quot; files in the backlog from \&quot; + $SendingMember + \&quot; to \&quot; + $ReceivingMember + \&quot;.`n\&quot;
}
#Write-Host + $Folder.ReplicatedFolderName \&quot;- \&quot; $BackLogFilecount -foregroundcolor $FGColor
}
}
else
{
Write-Host $ConnectionName \&quot;is not pingable\&quot;
$NoPingMessage = \&quot;Server \&quot;\&quot;\&quot; + $ConnectionName + \&quot;\&quot;\&quot; could not be reached.`nPlease verify it is on the network and pingable.\&quot;
Write-Event $EventSource $NoPingEventID \&quot;Warning\&quot; $NoPingMessage \&quot;Application\&quot;
}
}

}

}
## Write my events to the local Application log.

if ($SuccessAudit -ne $Null)
{
Write-Event $EventSource $SuccessEventID \&quot;Information\&quot; $SuccessAudit \&quot;Application\&quot;
}

if ($WarningAudit -ne $Null)
{
Write-Event $EventSource $WarningEventID \&quot;Warning\&quot; $WarningAudit \&quot;Application\&quot;
}

if ($ErrorAudit -ne $Null)
{
Write-Event $EventSource $ErrorEventID \&quot;Error\&quot; $ErrorAudit \&quot;Application\&quot;
}[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.043 secondes
Propulsé par Kunena