Why do my network shares disappear?

1,489

I assume you are talking about sending your files to something like, Z:\MyServer\FileRepository, where Z is your mapped drive? Will the utility work with a UNC path, such as \MyServer\c$\FileRepository?

EDIT: I realize I didn't answer the question, and only provided a workaround.

Share:
1,489

Related videos on Youtube

user1497590
Author by

user1497590

Updated on September 17, 2022

Comments

  • user1497590
    user1497590 almost 2 years

    I found this code online and wondering if it's easy to convert to Powershell. I am getting this error

    Missing expression after ','.
    At C:\AddaItem.ps1:60 char:73
    +         $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath, <<<<
    UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true)
        + CategoryInfo          : ParserError: (,:String) [], ParseException
        + FullyQualifiedErrorId : MissingExpressionAfterToken
    

    It appears to be complaining about this line:

    $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath,UTF8Encoding.UTF8.GetBytes(build
    er.ToString()), $true)  
    

    Here's the whole code:

    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
    {
        Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    $SiteUrl = "http://portal.memorial.hermann/patient"
    $web = Get-SPWeb $SiteUrl
    $Library = "Unpaid Billing Records"
    $docLibrary = $web.Lists[$Library]
    $rootFolder = $docLibrary.RootFolder
    $csvFile = "C:\\PowerShell\insurancefile.csv"
    # $fileURL
    
    foreach($i in Import-CSV $csvFile)
    {
        $sourceFile = Get-ChildItem $i.DocLink
        $destinationFolderPath = $rootFolder.Url
        if($i.DestinationFolder -ne ""){
            $destinationFolderPath += "/" + $i.DestinationFolder.replace("\","/")
    
            $folders = $i.DestinationFolder.Split("\")
    
            $subFolder = $rootFolder
            foreach($f in $folders)
            {
                $testFolder = $subFolder.SubFolders[$f];
                if($testFolder -eq $null)
                {
                    $subFolder = $subFolder.SubFolders.Add($f)
                    "created new folder " + $f
                }
                else
                {
                    $subFolder = $testFolder
                }
            }
        }
        $destinationFolderPath += "/" + $sourceFile.Name
    
        "copying " + $destinationFolderPath
        if($i.ContentType = "MasterDocument")
        {
            $itemType = $docLibrary.ContentTypes[$i.ContentType]
            $newFile = $docLibrary.RootFolder.Files.Add($destinationFolderPath,$sourceFile.OpenRead(), $true)
            $theItem = $newFile.Item
            $theItem["ContentTypeId"] = $itemType.Id
            # $fileURL = $theItem.Url
        }
        elseif($i.ContentType = "Link2Document")
        {
            $itemType = $docLibrary.ContentTypes[$i.ContentType]
            $newDestinationFolderPath = "/" + $i.fileNameASPX
            $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath,UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true)    
            $theItem = $newFile.Item
            $theItem["ContentTypeId"] = $itemType.Id
            $itemUrl = New-object Microsoft.SharePoint.SPFieldUrlValue      
            $itemUrl.Url = $i.fileLinkUrl
            $itemUrl.Descrition = $i.URLDESC
            $theItem["URL"] = $itemUrl      
        }
    
        # UPDATING METADATA 
        # $theItem["Name"] = $i.newfilename #Rename file name
        $theItem["Status"] = $i.Status
        $theItem["Title"] = $i.Title
        $theItem["Grantor"] = $i.Grantor
    
        $theItem.Update()
    }
    
    $web.Dispose()
    

    For reference, here's the C# code I'm trying to convert:

    using ( SPSite siteCollection = new SPSite( "http://moss.litwareinc.com" ) ) {
        using ( SPWeb web = siteCollection.OpenWeb( "docs" ) ) {
            SPList list = web.Lists["Sample"];
    
            //link to the file
            string fileLinkUrl = "http://moss.litwareinc.com/docs/Shared%20Documents/ConfigureIRMinWSS30.doc";
    
            StringBuilder builder = new StringBuilder();
    
            using ( TextReader reader = new StreamReader( @"C:\linktodocumenttemplate.txt" ) ) {
                builder.Append( reader.ReadToEnd() );
            }
    
            //replace string template with values
            builder.Replace( "{0}", fileLinkUrl );
    
            //should change the name of the .aspx file per item
            SPFile file = list.RootFolder.Files.Add( "link_title.aspx", UTF8Encoding.UTF8.GetBytes(builder.ToString()));
    
            //set list item properties
            SPListItem item = file.Item;
            item["Content Type"] = "Link to a Document";
            SPFieldUrlValue itemUrl = new SPFieldUrlValue();
            itemUrl.Description = "From sample code";
            itemUrl.Url = fileLinkUrl;
            item["URL"] = itemUrl;
            //persist changes
            item.Update();
        }
    }
    
    • Vivian River
      Vivian River over 14 years
      In response to Greg: Yes, the server is a cluster.
    • Kirk Woll
      Kirk Woll almost 12 years
      Please take the time to explain what "isn't working" and what specifically you need help with.
    • user1497590
      user1497590 almost 12 years
      Sorry, I totally forgot to add the error. I updated the post.
    • Kirk Woll
      Kirk Woll almost 12 years
      this is better, but please also include your powershell code.
    • user1497590
      user1497590 almost 12 years
      @KirkWoll: Yes sir. I have upload my powershell code here wikisend.com/download/429800/Add_Files_PS.txt
    • Kirk Woll
      Kirk Woll almost 12 years
      thanks, but in the future, please include the code inline as I've done for you.
    • Angshuman Agarwal
      Angshuman Agarwal almost 12 years
      Use the reflector add-on (if you have a licensed reflector) - blog.lekman.com/2011/10/converting-c-to-powershell.html
    • user1497590
      user1497590 almost 12 years
      @AngshumanAgarwal: I wish they would let me download reflector. I am still looking for some help. I am just stuck on few lines.
    • Angshuman Agarwal
      Angshuman Agarwal almost 12 years
  • Vivian River
    Vivian River over 14 years
    The admin for this server is very busy. I'm sure he could eventually get to the bottom of this. The reason I'm asking here is to get an idea where I could direct him to start. I imagine that this is likely caused by a script running somewhere. It is definitely possible that it is getting removed manually. What kinds of scripts might likely be responsible for removing my share? If it's being removed manually, is this action likely logged somewhere? These are the kinds of things I seek to find out.
  • mfinni
    mfinni over 14 years
    And it's only valid if he has rights to the c$ share, which generally means local admin on the server.
  • DanBig
    DanBig over 14 years
    I can only assume he has the rights, if he is able to connect and recreate the share.
  • Vivian River
    Vivian River over 14 years
    Well, apparently, the special account that I run my utility program does have rights to the C$ share. My primary role here is developer, not administrator, but I have been troubleshooting this problem for some time now. As I can access the files using the UNC path that you suggested, I have set up my utility program to access the files this way and everything works. I have other things to work on and this solution makes the program work now. Therefore, this is my accepted solution.
  • Vivian River
    Vivian River over 14 years
    Thanks. This utility is something that I've been extensively modifying the source code to. This problem happens about once every two weeks and I doubt that the utility itself could be directly causing this kind of problem.
  • mfinni
    mfinni over 14 years
    That won't have anything to do with the fact that he says he logs into the server, and sees that the folder is no longer shared.
  • Aashraya Singal
    Aashraya Singal over 14 years
    Gonna go pour me another large coffee.
  • user1497590
    user1497590 almost 12 years
    Thank you very much for your help.