|
Then the real problem is that you want to do something more complicated and that the code given doesn't provide an exact solution.
I would suppose that there is a way to do it.
I would also suppose that it will require reading the documentation, experimenting and guessing to get to a solution. Similar to other programming tasks that one might encounter.
|
|
|
|
|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) Hi,
Here is what I have, and so far this is working. It is far from finished and sorry for all of the comments, but I just got it working. It works for get-mailbox -identity first.last, it works for get-alias and it works for test-servicehealth. It itterates through everything, but it is a start
I'm sorry for the post length, I thought that codeblock would limit the screen usage and put a scrollable window within the post. If anyone knows how to do that, please let me know.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using exchangePowerShell;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
namespace TempTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
ExchangeManagementShellWrapper ems = ExchangeManagementShellWrapper.Instance;
ICollection<PSObject> results;
results = ems.RunspaceInvoke(textBox2.Text);
try
{
foreach (PSObject item in results)
{
List<string> propertyListTotal = new List<string>();
foreach (PSPropertyInfo itemSubProperty in item.Properties)
{
propertyListTotal.Add(itemSubProperty.ToString());
}
foreach (string propertyListItem in propertyListTotal)
{
string propertyListItemTrimBack = propertyListItem.Remove(propertyListItem.LastIndexOf(" "));
string propertyListItemTrimFront = propertyListItemTrimBack.Substring(propertyListItemTrimBack.LastIndexOf(" "));
propertyListItemTrimFront = propertyListItemTrimFront.TrimStart();
propertyListItemTrimFront = propertyListItemTrimFront.TrimEnd();
PSPropertyInfo propertyInfo = (PSPropertyInfo)item.Properties[propertyListItemTrimFront];
if (propertyInfo != null)
{
if (!(propertyInfo.TypeNameOfValue == "System.String[]") && !(propertyInfo.TypeNameOfValue.Contains("System.Nullable")))
{
if ((propertyInfo.Name != null) && (propertyInfo.Value != null))
{
textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + propertyInfo.Value.ToString());
textBox1.Text += System.Environment.NewLine;
}
else
{
textBox1.Text += ("Property has a null value");
textBox1.Text += System.Environment.NewLine;
}
}
if (propertyInfo.TypeNameOfValue == "System.String[]" && !(propertyInfo.TypeNameOfValue.Contains("System.Nullable")))
{
IList propertyArrayList = (IList)propertyInfo.Value;
if (propertyArrayList.Count == 0)
{
if ((propertyInfo.Name != null) && (propertyInfo.Value != null))
{
textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + propertyArrayList.Count);
textBox1.Text += System.Environment.NewLine;
}
else
{
textBox1.Text += ("Property has a null value");
textBox1.Text += System.Environment.NewLine;
}
}
if (propertyArrayList.Count > 0)
{
if ((propertyInfo.Name != null) && (propertyInfo.Value != null))
{
textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + propertyArrayList.Count);
textBox1.Text += System.Environment.NewLine;
foreach (string listItem in propertyArrayList)
{
textBox1.Text += ("Property name: " + propertyInfo.Name.ToString() + ", Property value: " + listItem);
textBox1.Text += System.Environment.NewLine;
}
}
else
{
textBox1.Text += ("Property has a null value");
textBox1.Text += System.Environment.NewLine;
}
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
namespace exchangePowerShell
{
public sealed class ExchangeManagementShellWrapper
{
#region Variable Declaration
private RunspaceConfiguration rc;
private Runspace r;
private RunspaceInvoke ri;
private static readonly ExchangeManagementShellWrapper instance = new ExchangeManagementShellWrapper();
#endregion
private ExchangeManagementShellWrapper(){}
public static ExchangeManagementShellWrapper Instance
{
get
{
return instance;
}
}
private void InitializeRunspace()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolver);
rc = RunspaceConfiguration.Create();
PSSnapInException warning;
PSSnapInInfo info = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
if (warning != null)
{
System.Console.Write(warning.Message);
return;
}
r = RunspaceFactory.CreateRunspace(rc);
r.Open();
}
private void InitializeRunspaceInvoke()
{
if (r == null) { InitializeRunspace(); }
ri = new RunspaceInvoke(r);
}
public ICollection<PSObject> RunspaceInvoke(string EMSCommand)
{
if (ri == null) { InitializeRunspaceInvoke(); }
return ri.Invoke(EMSCommand);
}
public ICollection<PSObject> RunspaceInvoke(string EMSCommand, IEnumerable input)
{
if (ri == null) { InitializeRunspaceInvoke(); }
return ri.Invoke(EMSCommand, input);
}
public ICollection<PSObject> RunspaceInvoke(string EMSCommand, out IList errors)
{
return RunspaceInvoke(EMSCommand, null, out errors);
}
public ICollection<PSObject> RunspaceInvoke(string EMSCommand, IEnumerable input, out IList errors)
{
if (ri == null) { InitializeRunspaceInvoke(); }
return ri.Invoke(EMSCommand, input, out errors);
}
public ICollection<PSObject> PipelineInvoke(Collection<Command> EMSCommands, out PipelineReader<object> Errors)
{
if (r == null) { InitializeRunspace(); }
Pipeline pipeline = r.CreatePipeline();
foreach (Command item in EMSCommands)
{
pipeline.Commands.Add(item);
}
ICollection<PSObject> results = pipeline.Invoke();
Errors = pipeline.Error;
pipeline = null;
return results;
}
public ICollection<PSObject> PipelineInvoke(Collection<Command> EMSCommands)
{
if (r == null) { InitializeRunspace(); }
Pipeline pipeline = r.CreatePipeline();
foreach (Command item in EMSCommands)
{
pipeline.Commands.Add(item);
}
return pipeline.Invoke();
}
public ICollection<PSObject> PipelineInvoke(Command EMSCommand, out PipelineReader<object> Errors)
{
if (r == null) { InitializeRunspace(); }
Pipeline pipeline = r.CreatePipeline();
pipeline.Commands.Add(EMSCommand);
ICollection<PSObject> results = pipeline.Invoke();
Errors = pipeline.Error;
pipeline = null;
return results;
}
public ICollection<PSObject> PipelineInvoke(Command EMSCommand)
{
if (r == null) { InitializeRunspace(); }
Pipeline pipeline = r.CreatePipeline();
pipeline.Commands.Add(EMSCommand);
return pipeline.Invoke();
}
public void Dispose()
{
r.Close();
ri = null;
r = null;
}
private static System.Reflection.Assembly AssemblyResolver(object p, ResolveEventArgs args)
{
if (args.Name.Contains("Microsoft.Exchange"))
{
return Assembly.LoadFrom(Path.Combine("C:\\Program Files\\Microsoft\\Exchange Server\\bin\\", args.Name.Split(',')[0] + ".dll"));
}
else
{
return null;
}
}
}
}
|
|
|
|
|
|
If the examples use a COM component you need to register it first (either by running its installer or on the command line with regsvr32).
|
|
|
|
|
I have this FTP code. I'm trying to get a list of files from the server. All it returns is an entry for the root folder.. Using FileZilla and/or CuteFTP I can see the subfolders and files in them. Why isnt't my returning anything other than the root folder?
public struct FTPCredentials
{
public string FTPServer { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public List<string> GetFileList(FTPCredentials Credentials)
{
List<string> retVal = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Credentials.FTPServer);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.KeepAlive = true;
request.UsePassive = false;
try
{
request.Credentials = new NetworkCredential(Credentials.UserName, Credentials.Password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string line = reader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
retVal.Add(line);
line = reader.ReadLine();
}
reader.Close();
response.Close();
}
catch (Exception e)
{
throw e;
}
return retVal;
}
Everything makes sense in someone's mind
|
|
|
|
|
FTP works similar to filesystem operations, so requesting anything from the server will work only in the current working directory. Since you're using FtpWebRequest, you'll also need to think of your FTP 'session' more in terms of HTTP style communications (browsing an FTP site using a web browser instead of an FTP client). You make one request, and you get one response. When you create the request:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
The variable 'uri' is actually both the server and the full path of your working directory. So if you want to connect to ftp.server.com and get the directory contents of path 'some/path', you would create your FtpWebRequest like so:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.server.com/some/path");
If you want to monitor how FTP actually communicates with ftp servers, enable the messages view in FileZilla (View > Message log). These are all happening implicitly when using the FtpWebRequest object.
I'd recommend finding an FTP library (or writing your own if you have the cohones, several RFCs make up the spec now) if you're going to be doing anything more than simple FTP operations.
modified on Thursday, June 16, 2011 4:29 PM
|
|
|
|
|
Thanks for the response. I think I understand. Some comments...
I set up a test FTP folder:
FTPServer = "ftp://ftp.maroisconsulting.com/test",
UserName = "FTPTest",
Password = "testftp"
When I run the code, I get the exception:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
I'm sorta lost here. I don't understand why this doesn't work. The folder exitst with 2 test files in it. My FTP Client (FileZilla )works, but not this code.
Everything makes sense in someone's mind
|
|
|
|
|
Based on the error number, your FTP client is connecting and authenticating without any problems. The problem seems to be realted to the server. It's possible that that access is denied to the file/directory (either by permission or file lock). If you have access to the server, verify the permissions for user FTPTest. You also might want to verify that the FtpWebRequest object credentials match your FileZilla settings exactly.
You could also try it against another FTP server you have access to and verify it. You code works for me when I tried it against both anonymous servers and my own.
|
|
|
|
|
Thanks Matt. That's good to know.
Everything makes sense in someone's mind
|
|
|
|
|
I am attempting to accomplish 2-way synch from a DataGridView to a table in my database. DGV to table is easy, but is there a "best practice" way to have the DGV automatically update to reflect any changes made to the database?
I tried using a timer and perform a TableAdapter.Fill(DataSet.SomeTable) at regular intervals, but the problem with this approach is that it "clobbers" any edit in progress, and resets the selected row to the top. Preferably, I would avoid impacting the user so directly.
Thank you...
EDIT: Here is how I am synching DGV changes to the database:
private void dgvSomeView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
myTableAdapter.Update(myDataSet.MyTable);
}
|
|
|
|
|
I think I found what I need: the SqlDependency class.
Looking into it now...
|
|
|
|
|
Can u help me optimize this functions? There are labels array5*5.
if Label[i,j] has symbol then IsEmpty[i,j] has false if it hasn't IsEmpty[i,j] is true;
Funcktions for game Royal Square )
public bool IsOpenForLetter(bool[,] IsEmpty, int x, int y)
{
int imin, imax, jmin, jmax;
int xSize = IsEmpty.GetLength(0);
int ySize = IsEmpty.GetLength(1);
if (IsEmpty[x, y])
{
if (x > 0) imin = x - 1;
else imin = x;
if (y > 0) jmin = y - 1;
else jmin = y;
if (x < xSize - 1) imax = x + 1;
else imax = x;
if (y < ySize - 1) jmax = y + 1;
else jmax = y;
for (int i = imin; i <= imax; i++)
for (int j = jmin; j <= jmax; j++)
if (!IsEmpty[i, j])
return true;
}
return false;
}
public bool IsItNear(int fx, int fy, int sx, int sy, bool[,] IsEmpty)
{
bool[,] Action = new bool[3, 3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
Action[i, j] = true;
int ftx, fty, sdx, sdy;
ftx = fx; fty = fy; sdx = sx; sdy = sy;
if (sdx == 0)
for (int i = 0; i < 3; i++)
Action[0, i] = false;
if (sdy == 0)
for (int i = 0; i < 3; i++)
Action[i, 0] = false;
if (sdx == 4)
for (int i = 0; i < 3; i++)
Action[2, i] = false;
if (sdy == 4)
for (int i = 0; i < 3; i++)
Action[i, 2] = false;
Action[1, 1] = false;
sdx--; sdy--;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (Action[i, j] == true)
if (IsEmpty[sdx, sdy] == false)
if (sdx == ftx && sdy == fty)
return true;
sdy++;
}
sdx++; sdy=sy-1;
}
return false;
}
|
|
|
|
|
This looks like homework...
|
|
|
|
|
It's part of my programm and programm is homework...
|
|
|
|
|
Why do you want us to do your homework for you? We'd better serve you by allowing you to learn for yourself... These are concepts that you will need to master, you know.
|
|
|
|
|
He showed he tried something , whether it is homework or not is irrelevant.
V.
|
|
|
|
|
What if the assignment was "optimize this (provided) code"?
Regardless, your other post seems to point him in the right direction.
|
|
|
|
|
If don't want to say how i can do it or give idea for it write this. I've written this this function for my programm but they are big. I'd like to know how i can do it use less actions. Maybe anyone has idea How i can do it.
|
|
|
|
|
I don't know this game, but here are some pointers:
1. Always use { and } even on single statements.
2. Avoid multiple returns
3. As I said, but why are you using and IsEmpty boolean array which I assume is 'synchronized' with the labels array? You can pass your labels array and check whether the label is empty or not. eg( string.IsNullOrEmpty(lbl_array[x,y].Text) )
4. You don't check if x and or y is within the array size, you can get an arrayindexoutofboundsexception error
5. You hardcode all variables, if you want to change the game to a 10*10 matrix you'll have a fun time changing all the variables.
6. make sure you name your variables so they mean something. I have no idea what fx, fy, sx, sy, ftx, fty, ... are for.
7. One statement, one line avoid: sdx--; sdy--;
this should help you already.
Don't mind the homework remark, you showed you tried something yourself, whether it is homework or not is irrelevant.
V.
|
|
|
|
|
Those don't really have anything to do with optimisation, and most of them (1, 2, 6 and 7) are matters of opinion, of which I only agree with one (6, and even then, those names might well be meaningful if you know the game).
|
|
|
|
|
BobJanova wrote: Those don't really have anything to do with optimisation
Depends on what you mean with optimisation.
BobJanova wrote: which I only agree with one
You don't have to agree, but I wouldn't want to debug your code if you write it like that. (I'm not saying your code is bad nor is it wrong)
V.
|
|
|
|
|
BobJanova wrote: of which I only agree with one
Then you are missing good code standards used by most professionals
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I'm not missing them, I just don't agree with them
(Or, to be less glib, they come from good principles, but when applied as fixed rules, they make things less readable, generally by expanding the code so that you can't see the pattern of it any more. In the case of the example posted here, decrementing both the x and y variables in one line makes sense because it's one thought, for example.)
There is nothing wrong with multiple returns, though. Checking a condition within a foreach loop and returning when it's hit is quite common and perfectly readable (arguably more so than assigning to a variable and break ing since you can see that it's an exit point).
|
|
|
|
|
To each there own. There is no learning when the mind is closed. Hope I don't have to pick any code you have been working on.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
"There is no learning when the mind is closed."
Quite. I'm glad you agree with me that fixed rules such as those I was responding to are not a good idea, and one should keep an open mind as to what the situation you're coding for requires.
|
|
|
|
|