private T RunActionWithUncConnectionIfNeeded <T>(Func <T> action)
        {
            T result = default(T);

            if (this.IsNetworkPath.Value && this.Credential != null)
            {
                using (UncNetworkConnector connector = new UncNetworkConnector())
                {
                    NetworkCredential networkCredential = this.Credential.GetNetworkCredential();
                    if (connector.NetUseWithCredentials(this.Path, networkCredential.UserName, networkCredential.Domain, networkCredential.Password))
                    {
                        result = action();
                    }
                    else
                    {
                        string errorMessage = connector.GetLastError();
                        WriteError(new ErrorRecord(new Exception($"Failed mounting network path {this.Path}. Error: {errorMessage}"), errorMessage, ErrorCategory.ConnectionError, this.Path));
                    }
                }
            }
            else
            {
                result = action();
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the action with unc connection if needed.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action">The action.</param>
        /// <returns>T.</returns>
        private T RunActionWithUncConnectionIfNeeded <T>(Func <T> action)
        {
            T result = default(T);

            if (IsNetworkPath.Value && Credential != null)
            {
                using (UncNetworkConnector connector = new UncNetworkConnector())
                {
                    NetworkCredential networkCredential = Credential.GetNetworkCredential();
                    if (connector.NetUseWithCredentials(Path, networkCredential.UserName, networkCredential.Domain, networkCredential.Password))
                    {
                        result = action();
                    }
                    else
                    {
                        string errorMessage = connector.GetLastError();
                        WriteError(new ErrorRecord(new Exception(
                                                       string.Format(StorageSyncResources.InvokeCompatibilityCheckError1Format, Path, errorMessage)), errorMessage, ErrorCategory.ConnectionError, Path));
                    }
                }
            }
            else
            {
                result = action();
            }

            return(result);
        }